Similar to C #, a custom event requires a custom eventargs subclass. as3 also requires the developer to define a subclass of the event class. Here we assume a scenario: design a person (person) class, which includes age (AGE) and name (name). We hope that some custom events can be triggered whenever the age (AGE) of the person class changes, to call some specific processing methods.
1. First design the sub-class agechangeevent of the event class
Package {import flash. events. event; public class agechangeevent extends event {public static const age_out_of_range: String = "age_out_of_range"; public static const age_change: String = "age_change"; Public Function agechangeevent (eventtype: string) {super (eventtype );}}}
Here we define the second type of event: Age Change (age_change), age out of range (age_out_of_range)
2. design the person class again.
Package {import flash. display. sprite; public class person extends sprite {private VaR _ age: uint; private VaR _ personname: string; Public Function person (personname: String, age: uint) {This. _ age = age; this. _ personname = personname;} // encapsulate _ age as the public function get age (): uint {return _ age ;} // setter method of the age attribute (add event Dispatch here) public function set age (age: uint): void {If (age! = This. _ age) {VaR _ agechangeevent1: agechangeevent = new agechangeevent (agechangeevent. age_change); dispatchevent (_ agechangeevent1); // trigger the age change event} If (age> = 120) {VaR _ agechangeevent2: agechangeevent = new agechangeevent (agechangeevent. age_out_of_range); dispatchevent (_ agechangeevent2); // trigger an event whose age exceeds the normal range} This. _ age = age;} // override the tostring method of the parent class public override function tostring (): String {return "" + this. _ personname + "" shoes have been "" + this year. _ age + "" years old. It's really not a year old. ";}}}
Note that in the dispatchevent Processing Section, define an event (parameter) object and dispatch the event. (similar to the Delegate/event mechanism in C #, the person class does not know how the user of the final peson will handle these two events, but it can correctly call the processing method executed by the developer as long as it complies with the conventions)
3. Test the event.Create a FLA file for flash file (actionscript3.0) and write the following content to the first frame.Code:
VaR JIMMY: person = new person ("Yang Guo under the bodhi tree", 30); trace (Jimmy. tostring (); Jimmy. age = 31; // No event has been registered, so nothing will be triggered. // register the event Jimmy. addeventlistener (agechangeevent. age_change, agechangehandler); function agechangehandler (E: agechangeevent): void {trace ("NOTE: This guy's age has changed! ");} Jimmy. age = 32; // The agechangehandler event Jimmy is triggered when the current age is changed. age = 150; // The agechangehandler event will be triggered again // register another event Jimmy. addeventlistener (agechangeevent. age_out_of_range, ageoutofrangehandler); function ageoutofrangehandler (E: agechangeevent): void {trace ("note: this old guy says he is over 120 years old! ");} Jimmy. Age = 149; // The age_change and age_out_of_range events are triggered simultaneously.
It is recommended that you first guess the result and practice it, and then look at the output below:
The shoes of Yang Guo under the bodhi tree are already "30" years old this year.
Note: This guy's age has changed!
Note: This guy's age has changed!
Note: This guy's age has changed!
Note: This old guy says he is over 120 years old!
Sometimes, we also want the agechangeevent parameter to include some additional information, which makes it hard to replace the AS and transform the agechangeevent (note the new part ):
Package {import flash. events. event; public class agechangeevent extends event {public static const age_out_of_range: String = "age_out_of_range"; public static const age_change: String = "age_change"; // Add some private VaR _ oldage: uint, _ newage: uint; Public Function agechangeevent (eventtype: String, oldage: uint, newage: uint) {This. _ oldage = oldage; this. _ newage = newage; Super (eventtype);} public function get oldage (): uint {return _ oldage;} public function get newage (): uint {return _ newage ;}}}
The setter section of age in the person class should also be changed:
// Setter method of the age attribute (add event Dispatch here) public function set age (age: uint): void {If (age! = This. _ age) {VaR _ agechangeevent1: agechangeevent = new agechangeevent (agechangeevent. age_change, this. _ age, age); dispatchevent (_ agechangeevent1); // triggers the age change event} If (age >=120) {VaR _ agechangeevent2: agechangeevent = new agechangeevent (agechangeevent. age_out_of_range, this. _ age, age); dispatchevent (_ agechangeevent2); // trigger an event with age exceeding the normal range} This. _ age = age ;}
Slightly changed the code in the FLA test:
VaR JIMMY: person = new person ("Yang Guo under the bodhi tree", 30); trace (Jimmy. tostring (); Jimmy. age = 31; // No event has been registered, so nothing will be triggered. // register the event Jimmy. addeventlistener (agechangeevent. age_change, agechangehandler); function agechangehandler (E: agechangeevent): void {trace ("NOTE: This guy ages from" + E. oldage + "changed to" + E. newage + "now! ");} Jimmy. age = 32; // The agechangehandler event Jimmy is triggered when the current age is changed. age = 150; // The agechangehandler event will be triggered again // register another event Jimmy. addeventlistener (agechangeevent. age_out_of_range, ageoutofrangehandler); function ageoutofrangehandler (E: agechangeevent): void {trace ("note: this old guy says he is over 120 years old and now has" + E. newage + "now! ");} Jimmy. Age = 149; // The age_change and age_out_of_range events are triggered simultaneously.
New output result:
The shoes of Yang Guo under the bodhi tree are already "30" years old this year.
Note: This guy's age has changed from 31 to 32!
Note: This guy's age has changed from 32 to 150!
Note: This guy's age has changed from 150 to 149!
Note: The old guy said he was over 120 years old and now he is 149 years old!
Postscript:Custom events can be widely used in many scenarios. For example, we can create a control slider movieclip, which defines a valuechange event and other places (such as volume control and object Alpha value control, object Size Control ...), you only need to define your own processing function and register it to the event.
Source code: Http://files.cnblogs.com/yjmyzz/as3_custom_events.rar
Finally, as a comparison and review, we will post the custom events for C:
Agechangeeventargs class
Using system; namespace eventdemo {public class agechangeeventargs: eventargs {private uint _ oldage; private uint _ newage; Public agechangeeventargs (uint oldage, uint newage) {This. _ oldage = oldage; this. _ newage = newage;} public uint oldage {get {return _ oldage;} public uint newage {get {return _ newage ;}}}}
Person class
Namespace eventdemo {public class person {Public Delegate void agechangeeventhandler (Object sender, agechangeeventargs E); public event languagechangeevent; public event agechangeeventhandler identifier; private uint _ age; private string _ name; public uint age {get {return age;} set {If (value! = _ Age) {If (agechangeevent! = NULL) {agechangeevent (this, new agechangeeventargs (_ age, value) ;}} if (value >=120) {If (ageoutofrangeevent! = NULL) {ageoutofrangeevent (this, new agechangeeventargs (_ age, value) ;}}_ age = value ;}} public string name {get {return _ name ;} set {_ name = value ;}} public person (string pname, uint page) {This. _ age = page; this. _ name = pname;} public override string tostring () {return "" + this. _ name + "" this year "+ this. _ age + "" years old! ";}}}
TestProgram:
Using system; namespace eventdemo {class program {static void main (string [] ARGs) {person P = new person ("Yang Guo under the bodhi tree", 30); console. writeline (P. tostring (); p. age = 31; p. agechangeevent + = new person. agechangeeventhandler (p_agechangeevent); p. age = 32; p. ageoutofrangeevent + = new person. agechangeeventhandler (p_ageoutofrangeevent); p. age = 150; console. readkey ();} static void p_ageoutofrangeevent (OBJ ECT sender, agechangeeventargs e) {console. writeline ("Oh, this guy's age has reached" + E. newage +! ");} Static void p_agechangeevent (Object sender, agechangeeventargs e) {console. writeline ("NOTE: This guy's age is from" + E. oldage + "changed to" + E. newage + "years old! ");}}}