Transferred from the Internet.
Introduce the application of custom events in flash as with the example of boys and girls
Package {
Import flash. Events. event;
// Import event class
Public class customevent extends event {
// Declare Custom Event extended Custom Event class as its subclass
Public static const sendflower: String = " Sendflower " ;
// Declare static constants as Event Type 1
Public static const sendcar: String = " Sendcar " ;
// Declare static constants as event type 2
Public VaR Info: string;
// Declare variables to store event information, which is also the main reason for using custom events.
// Carrying Additional information
Public Function Customevent (type: String, INF ){
Super (type );
// Call the parent class constructor and set the input parameters as the event type.
Info = INF;
// Store input parameter 2 in info
}
}
}
// 2. Boys:
Package {
Import flash. Events. eventdispatcher;
// Import event sender class
Import flash. Events. event;
// Import event class
Public class boy extends eventdispatcher {
// Declare the boy class to expand from the event sender class and become its subclass
Public Function Sendflower (){
// Declare the public sending method;
VaR Info: String = " Rose " ;
// Declare local variables and set sending information
VaR Events = New Customevent (customevent. sendflower, Info );
// Declare an instance of the New Custom Event class, and set the type to the first,
// Store the sent information to the event
This . Dispatchevent (events );
// Send this event
}
Public Function Sendcar (){
// Declare Public delivery methods;
VaR Info: String = " Millions of sports cars " ;
// Declare local variables and set sending information
VaR Events = New Customevent (customevent. sendcar, Info );
// Declare an instance of the New Custom Event class, and set the type to second,
// Store the sent information to the event
This . Dispatchevent (events );
// Send this event
}
}
}
// 3. GIRLS:
Package {
Public class girl {
Public Function Replay (Info ): Void {
Trace (Info );
}
// Declare Public methods and respond
}
}
// 4. Document Type:
Package {
Import flash. display. Sprite;
// Import sprite class
Public class documents extends sprite {
// Declaration Document class extended from sprite class
Private VaR _ BOY: boy;
// Declare the private property as the boy type
Private VaR _ GIRL: girl;
// Declare private property as girl type
Public Function Documents (){
// Constructor
_ Boy = New Boy;
// Create a boy instance
_ Girl = New Girl;
// Create girl instance
_ Boy. addeventlistener (customevent. sendflower, _ Hand );
// Add customevent. sendflower event listening type for boys
_ Boy. addeventlistener (customevent. sendcar, _ Hand );
// Add customevent. sendcar event listening type for boys
_ Boy. sendcar ();
// Call the boy's delivery method. You can try again to call the boy's sendflower
// Try the method to see how the results are different.
}
Private Function _ Hand (E: customevent ): Void {
// Declare event Processor
_ Girl. Replay ( " I received a handsome guy who gave me the following: " + E.info );
// Call the girl's replay method.
}
}
}