Event mechanism in the basics of ActionScript 3.0

Source: Internet
Author: User

In this case, we will summarize the event mechanism in the event 3.0. Here we will only talk about the method 3 in the Custom class to send events, because it is commonly used in the design mode. For example, MVC.

There are two main types of custom events: Event and EventDispatcher ). The custom sending event we discuss is how to enable an object to send events.

Method 1: Inherit the EventDispatcher class

Package {import flash. display. sprite; import flash. events. event; import flash. events. eventDispatcher ;/***... * @ author oak hut */public class estends extends Sprite {function estends () {// first var dispatcher: extendsDispatcher = new extendsDispatcher (); dispatcher. addEventListener (extendsDispatcher. ACTION, actionH); dispatcher. doSome (); // var dispatcher1: EventDispatcher = new EventDispatcher (); events ("secDispatcher", actionH); dispatcher1.dispatchEvent (new Event ("secDispatcher ")); // or dispatcher. addEventListener ("thurdDispatcher", actionH); dispatcher. dispatchEvent (new Event ("thurdDispatcher");} private function actionH (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; class extendsDispatcher extends EventDispatcher {public static var ACTION: String = "action"; public function doSome (): void {dispatchEvent (new Event (extendsDispatcher. ACTION ));}}

As long as you inherit the EventDispatcher class, you can directly use the EventDispatcher class method to send events, which is convenient and simple.

Method 2: Composite EventDispatcher object

Package {/***... * @ author oak hut */import flash. display. sprite; import flash. events. event; public class compont extends Sprite {public function compont () {var dis: compont1 = new compont1 (); dis. getEventDispatch (). addEventListener (compont1.ACTION, show); dis. doSome ();} private function show (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; class compont1 {private var _ dispatcher: EventDispatcher; public static var ACTION: String = "action"; public function compont1 () {_ dispatcher = new EventDispatcher ();} public function getEventDispatch (): EventDispatcher {return _ dispatcher;} public function doSome (): void {_ dispatcher. dispatchEvent (new Event (compont1.ACTION ));}}

The so-called combination is actually to use EventDispatcher to send events, because EventDispatcher itself is an event sending class. Generally, it is easier to use EventDispatcher to send events.

Method 3: implement the IEventDispatcher Interface

Package {import flash. display. sprite; import flash. events. event ;/***... * @ author oak hut */public class implement extends Sprite {public function implement () {var impObj: implete = new implete (); impObj. addEventListener (implete. ACTION, actionH); impObj. dispatchEvent (new Event (implete. ACTION);} private function actionH (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; import flash. events. IEventDispatcher; class implete implements IEventDispatcher {public var _ dispatcher: EventDispatcher; public static const ACTION: String = "action"; public function implete () {_ dispatcher = new EventDispatcher ();} // implement five methods: public function addEventListener (type: String, listener: Function, useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = true ): void {_ dispatcher. addEventListener (type, listener, useCapture, priority, useWeakReference);} public function dispatchEvent (evt: Event): Boolean {return _ dispatcher. dispatchEvent (evt);} public function hasEventListener (type: String): Boolean {return _ dispatcher. hasEventListener (type);} public function removeEventListener (type: String, listener: Function, useCapture: Boolean = false): void {_ dispatcher. removeEventListener (type, listener, useCapture);} public function willTrigger (type: String): Boolean {return _ dispatcher. willTrigger (type );}}

When the class itself needs to inherit other classes and cannot inherit the EventDispatcher class, it also hopes that this class can become a simple event sending class, and you can add some methods in it by yourself, at this time, it is more appropriate to inherit the IEventDispatcher interface, but the process is more difficult to write. Five methods to implement the IEventDispatcher interface are required.

 

 

References:

The road to the temple of flash ActionScript 3.0

Turn: http://www.cnblogs.com/babyzone2004/archive/2010/10/03/1841509.html

 

In this case, we will summarize the event mechanism in the event 3.0. Here we will only talk about the method 3 in the Custom class to send events, because it is commonly used in the design mode. For example, MVC.

There are two main types of custom events: Event and EventDispatcher ). The custom sending event we discuss is how to enable an object to send events.

Method 1: Inherit the EventDispatcher class

Package {import flash. display. sprite; import flash. events. event; import flash. events. eventDispatcher ;/***... * @ author oak hut */public class estends extends Sprite {function estends () {// first var dispatcher: extendsDispatcher = new extendsDispatcher (); dispatcher. addEventListener (extendsDispatcher. ACTION, actionH); dispatcher. doSome (); // var dispatcher1: EventDispatcher = new EventDispatcher (); events ("secDispatcher", actionH); dispatcher1.dispatchEvent (new Event ("secDispatcher ")); // or dispatcher. addEventListener ("thurdDispatcher", actionH); dispatcher. dispatchEvent (new Event ("thurdDispatcher");} private function actionH (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; class extendsDispatcher extends EventDispatcher {public static var ACTION: String = "action"; public function doSome (): void {dispatchEvent (new Event (extendsDispatcher. ACTION ));}}

As long as you inherit the EventDispatcher class, you can directly use the EventDispatcher class method to send events, which is convenient and simple.

Method 2: Composite EventDispatcher object

Package {/***... * @ author oak hut */import flash. display. sprite; import flash. events. event; public class compont extends Sprite {public function compont () {var dis: compont1 = new compont1 (); dis. getEventDispatch (). addEventListener (compont1.ACTION, show); dis. doSome ();} private function show (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; class compont1 {private var _ dispatcher: EventDispatcher; public static var ACTION: String = "action"; public function compont1 () {_ dispatcher = new EventDispatcher ();} public function getEventDispatch (): EventDispatcher {return _ dispatcher;} public function doSome (): void {_ dispatcher. dispatchEvent (new Event (compont1.ACTION ));}}

The so-called combination is actually to use EventDispatcher to send events, because EventDispatcher itself is an event sending class. Generally, it is easier to use EventDispatcher to send events.

Method 3: implement the IEventDispatcher Interface

Package {import flash. display. sprite; import flash. events. event ;/***... * @ author oak hut */public class implement extends Sprite {public function implement () {var impObj: implete = new implete (); impObj. addEventListener (implete. ACTION, actionH); impObj. dispatchEvent (new Event (implete. ACTION);} private function actionH (e: Event): void {trace (e) ;}} import flash. events. event; import flash. events. eventDispatcher; import flash. events. IEventDispatcher; class implete implements IEventDispatcher {public var _ dispatcher: EventDispatcher; public static const ACTION: String = "action"; public function implete () {_ dispatcher = new EventDispatcher ();} // implement five methods: public function addEventListener (type: String, listener: Function, useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = true ): void {_ dispatcher. addEventListener (type, listener, useCapture, priority, useWeakReference);} public function dispatchEvent (evt: Event): Boolean {return _ dispatcher. dispatchEvent (evt);} public function hasEventListener (type: String): Boolean {return _ dispatcher. hasEventListener (type);} public function removeEventListener (type: String, listener: Function, useCapture: Boolean = false): void {_ dispatcher. removeEventListener (type, listener, useCapture);} public function willTrigger (type: String): Boolean {return _ dispatcher. willTrigger (type );}}

When the class itself needs to inherit other classes and cannot inherit the EventDispatcher class, it also hopes that this class can become a simple event sending class, and you can add some methods in it by yourself, at this time, it is more appropriate to inherit the IEventDispatcher interface, but the process is more difficult to write. Five methods to implement the IEventDispatcher interface are required.

 

 

References:

The road to the temple of flash ActionScript 3.0

Turn: http://www.cnblogs.com/babyzone2004/archive/2010/10/03/1841509.html

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.