ActionScript - Display Object With Multiple Mouse Events?
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.native开发者_开发百科Window.y != windowCoords.y)
return;
//expand & collapse code
}
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
Yes, but you have to be careful with the order of declaration of your events (not sure which one should go first, though...).
精彩评论