ActionScript-3 – Can I use the mouse down and up an event analog mouse click?

A previous question made me think about whether it is possible to simulate MouseEvent.CLICK being triggered by first triggering MOUSE_DOWN and then MOUSE_UP.

According to Adobe’s recommendation Text.
“…For the click event to occur, it must always follow this series of events in the order of occurrence: mouseDown event, then mouseUp. The target objects of these two events must be the same; otherwise, the click event will not occur. Any number of other mouse events can occur at any time between mouseDown or mouseUp events; click events will still occur.” http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display /InteractiveObject.html#event:click

From my test, we can see that the CLICK event is not constructed from the ActionScript 3 event queue. Or is there a problem with the code?

See:

package
{
import flash.display.Sprite;
import flash.events. Event;
import flash.events.MouseEvent;

[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]< br />
public class Test extends Sprite
{
private var spr:Sprite = new Sprite();
public function Test()
{
trace("Test()");
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void
{
trace("init()");
spr.graphics.beginFill(0xFF0000);
spr.graphics.drawRect(0,0,200,80);
spr.graphics.endFill ();
addChild(spr);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
spr.addEventListener(MouseEvent.CLICK, onClick);
}
private var tick:int = 0;
private function onEnterFrame(e:Event):void
{
if (tick == 1) spr.dis patchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
tick++;
}
private function onClick(e:MouseEvent):void
{
trace("onClick() MouseEvent.CLICK dispatched");
}
}
}

I should get two’onClick()’ Event instead of one.

the reason you can’t do this is that all three types are made by plugins Created and do not depend on each other.

MouseEvent.MOUSE_DOWN will be triggered immediately after pressing the interactive object.
MouseEvent.MOUSE_UP will be triggered immediately after releasing the mouse, it does not depend on the mouse Click has initiated MOUSE_DOWN in the same InteractiveObject.
MouseEvent.CLICK is only triggered when two events occur in the same object, and the cursor does not leave an object between the mouse down and mouse up objects.
p>

So you can see that there is a simple situation, or two even numbers, where both MOUSE_DOWN and MOUSE_UP are triggered, but CLICK is not called because the event is not CLICK.

In addition, simply The ability to dispatch MouseEvent.CLICK events makes it unnecessary to create fake user interactions by triggering multiple mouse events (only one mouse event is called). It is awkward to have to track every object clicked in the mouse press listener In order to check that the moue should also trigger a click. When the ROLL_OUT event occurs, you also need to delete the reference.

In short, CLIC The K event actually far exceeds the MOUSE_DOWN and MOUSE_UP events. It also manages the continuous occurrence of events on the same display object without leaving the object. When this happens, flash will dispatch the CLICK event only when this happens.

A previous question made me think. Is it possible to simulate MouseEvent by first triggering MOUSE_DOWN and then MOUSE_UP. CLICK is triggered.

According to Adobe’s tweet. “…For click events to occur, it must always follow this series of events in the order in which they occur: mouseDown event, then mouseUp. The target objects of these two events must be the same; otherwise not The click event will occur. Any number of other mouse events can occur at any time between mouseDown or mouseUp events; the click event will still occur.” http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3 /flash/display/InteractiveObject.html#event:click

From my test, we can see that the CLICK event is not constructed from the ActionScript 3 event queue. Or is there a problem with the code?

See:

package
{
import flash.display.Sprite;
import flash.events. Event;
import flash.events.MouseEvent;

[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]< br />
public class Test extends Sprite
{
private var spr:Sprite = new Sprite();
public function Test()
{
trace("Test()");
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void
{
trace("init()");
spr.graphics.beginFill(0xFF0000);
spr.graphics.drawRect(0,0,200,80);
spr.graphics.endFill ();
addChild(spr);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
spr.addEventListener(MouseEvent.CLICK, onClick);
}
private var tick:int = 0;
private function onEnterFrame(e:Event):void
{
if (tick == 1) spr.dispat chEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
tick++;
}
private function onClick(e:MouseEvent):void
{
trace("onClick() MouseEvent.CLICK dispatched");
}
}
}

I should get two’onClick()’ Event instead of one.

The reason you can’t do this is that all three types are created by plugins and do not depend on each other.

MouseEvent.MOUSE_DOWN will be triggered immediately after the interactive object is pressed.
MouseEvent.MOUSE_UP will be triggered immediately after the mouse is released, it does not depend on the mouse click that has started MOUSE_DOWN in the same InteractiveObject.
MouseEvent.CLICK is only triggered when two events occur in the same object, and the cursor will not leave an object between the mouse down and mouse up objects.

So you can see that there is one Simple cases, or two even numbers, where both MOUSE_DOWN and MOUSE_UP are triggered, but CLICK is not called because the event is not CLICK.

In addition, the ability to simply dispatch MouseEvent.CLICK events makes it possible to trigger multiple Mouse events (only one mouse event is called) to create fake user interactions are unnecessary. It is awkward to have to track every object clicked in the mouse down listener so that checking the moue should also trigger the click. When When the ROLL_OUT event occurs, you also need to delete the reference.

In short, the CLICK event actually far exceeds the MOUSE_DOWN and MOUSE_UP events. It also manages the continuous occurrence of events on the same display object without leaving the object .When hair When this happens, flash will dispatch the CLICK event only when this happens.

Leave a Comment

Your email address will not be published.