Actionscript 3.0 Mouse Event _flash AS3

Source: Internet
Author: User
About the event mechanism of ActionScript 3.0, the next module is an introduction, the final form of a complete document. Read the first chapter, talk about mouse events, by the way I encountered a problem. Actionscript 3.0 mouse events are nothing more than listening and capturing mouse events, such as Click,doubleclick, AS3 the mouse event package is in Flash.events.MouseEvent.
Here is a simple ActionScript 3.0 to implement WordPad code, combined with the code to understand the monitoring of the mouse, capturing the processing process.
Copy Code code as follows:

Package
{
Import Flash.display.Sprite;
Import flash.events.Event;
Import flash.events.MouseEvent; Import MouseEvent Package
/**
* @Written by Leezhm, 6th June, 2009
* @Contact: Leezhm@126.com
* @author: LEEZHM
*
**last Modified by Leezhm on 6th June, 2009
*
*/
[SWF (height = "450", Width = "$", BackgroundColor = "0xFFFFFF", framerate = "31")]//Set application properties
public class Main extends Sprite
{
Public Function Main (): void
{
if (stage)
{
Init ();
}
Else
{
AddEventListener (Event.added_to_stage, Init);
}
}
Private Function Init (e:event = null): void
{
RemoveEventListener (Event.added_to_stage, Init);
Entry point
AddEventListener (Event.enter_frame, Onenterframehandler); Monitor Enter_frame events, an important event
}
Private Function Onenterframehandler (e:event): handler function for void//enter_frame event
{
This. Drawgraphic ();
}
Private Function drawgraphic (): void
{
This.stage.addEventListener (Mouseevent.mouse_down, Onmousedownhandler); Monitor Mouse down Events
}
Private Function Onmousedownhandler (e:mouseevent): void//Handle mouse down event
{
This.graphics.lineStyle (2, 0, 1);
This.graphics.moveTo (This.mousex, This.mousey);
This.stage.addEventListener (Mouseevent.mouse_move, Onmousemovehandler); Monitor Mouse Move Events
}
Private Function Onmousemovehandler (e:mouseevent): void//mouse move event handler
{
This.graphics.lineTo (This.mousex, This.mousey);
This.stage.addEventListener (mouseevent.mouse_up, Onmouseuphandler); Monitor Mouse up events
}
Private Function Onmouseuphandler (e:mouseevent): void//Handle mouse up event
{//Remove monitoring of mouse down, move, and up events
This.stage.removeEventListener (Mouseevent.mouse_down, Onmousedownhandler);
This.stage.removeEventListener (Mouseevent.mouse_move, Onmousemovehandler);
This.stage.removeEventListener (mouseevent.mouse_up, Onmouseuphandler);
}
}
}

In fact, looking at the above code is very simple to understand the mouse events in ActionScript 3.0 processing process, passing a picture of the effect.

Well, say one of the mistakes I've made, the code is as follows:
Copy Code code as follows:

Package
{
Import Flash.display.Sprite;
Import flash.events.MouseEvent;
public class A extends Sprite
{
Public Function A ()
{
Graphics.beginfill (0xff0000);
Graphics.drawcircle (100,100,40);
Graphics.endfill ();
AddEventListener (Mouseevent.click, Testclick);
}
Private Function Testclick (event:mouseevent): void {
Trace ("Hello world!!!");
}
}
}

In fact, the above code does not respond to the mouse click event, why, at first I also spent a long time to find out why. In fact, this involves the event implementation mechanism of ActionScript 3.0, which is briefly explained here:
The reason is simple, because a document class like the one above is empty, there is no display object at all, so there is no response to the mouse event (that drawcircle is not a display object, it is only a background, not in the display list of ActionScript, Therefore, it is not possible to distribute event messages to it in the ActionScript event mechanism. But the following for the above code can be modified.
Copy Code code as follows:

Package {
Import Flash.display.Sprite;
Import flash.events.MouseEvent;
public class A extends Sprite {
Public Function A () {
var _sp:sprite=new Sprite ()
_sp.graphics.beginfill (0xff0000);
_sp.graphics.drawcircle (100,100,40);
_sp.graphics.endfill ();
AddChild (SP)
_sp.addeventlistener (Mouseevent.click, Testclick);
}
Private Function Testclick (event:mouseevent): void {
Trace ("Hello world!!!");
}
}
}

Comparing the two code can be found, followed by the addition of a display object, and on the Display object monitor mouse events. Of course, you can also want to listen on the stage in the first paragraph of code.
Note that the direct this.addeventlistener this to monitor is root, is not stage such Displayobject object, also is not possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.