views:

21

answers:

2

i've created a simple panel with a title bar, and i'm trying to share the title bar between MouseEvent.MOUSE_DOWN, MouseEvent.MOUSE_CLICK and MouseEvent.DOUBLE_CLICK. oh mouse down, the panel is draggable, on mouse click the panel collapses and expands, and on mouse double click the panel is hidden.

i'm struggling to make these all work with the same title bar sprite of the panel. mouse down activates a click when the mouse is up, etc. is it possible to have these mouse events distinguishable on the same object?


i forgot to mention that i'm programming an AIR application, so while i believe PatrickS's solution below would work for a regular .swf file or one that has custom drag and drop functions, i don't really have access to the nativeWindow's startMove() function. however, i've managed to share the panel's titleBar object between MOUSE_DOWN and MOUSE_CLICK events by polling for the position of the nativeWindow.

private function titleBarMouseDownEventHandler(evt:MouseEvent):void
    {
    windowCoords = new Point(stage.nativeWindow.x, stage.nativeWindow.y);
    stage.nativeWindow.startMove();
    }

private function titleBarClickEventHandler(evt:MouseEvent):void
    {
    if  (stage.nativeWindow.x != windowCoords.x && stage.nativeWindow.y != windowCoords.y)
        return;

    //expand & collapse code
    }
+1  A: 

yes you can check the event type and add a conditional for your function to execute


private function mouseEventHandler(event:MouseEvent):void
{
    switch( event.type )
    {
         case MouseEvent.MOUSE_DOWN:
               dragClip();
               break;

         case MouseEvent.CLICK:
               removeListeners();
               clickHandler();
               break;
     }

}

private function dragClip():void
{
   //remove listeners while you're dragging
   //add them back on mouse up
   removeListeners(); 
}

private function clickHandler():void
{ 
     //do what you need
     //then re-add the event listeners when the action is done
     addListeners();
}

if you still can't distinguish using this method, you may have to create a second handler for the conflicting functions... come to think of it , you could also consider disabling other mouse event handlers when one is active... just a thought

PatrickS
sure, i can send them all to their own listener functions or check for which event type is activated, but the problem is that one sets off another one. for example: i mouse down on the panel to drag it to a new location, but when i let go of the mouse the panel will expand/collapse, since the expand/collapse action is listening for a mouse click. i've tried removing the click event listener for mousedown and re-adding it in mouseup, but the outcome to that is that the click event never happens.
TheDarkInI1978
you may have to establish some form of priority, let's say that you use a switch statement , using event.type , the first statement would take precedence
PatrickS
the only other thing i can think of is to use timers, but that would be super ghetto.
TheDarkInI1978
PatrickS
i think perhaps your solution would work in a regular .swf, but i'm actually programming an AIR application, so window dragging is a little different (i don't really have access to the drag function). i've posted a solution to my original question.
TheDarkInI1978
fair enough , i don't know much about AIR ... maybe the principle of disabling the remaining event listeners when one is active is still possible to implement...
PatrickS
A: 

Yes, but you have to be careful with the order of declaration of your events (not sure which one should go first, though...).

Raveline