Custom events in Flash as3.0

Source: Internet
Author: User

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.

// Carry 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 the input parameter 2 to Info

}

}

}

// 2. Boys:
Package {

Import flash. Events. eventdispatcher;

// Import the event sender class

Import flash. Events. event;

// Import event class

Public class boy extends eventdispatcher {

// Declare that the boy class extends from the event sender class to become its subclass

Public Function sendflower (){

// Declare the public sending method;

VaR info: String = "Rose ";

// Declare the sending information of the local variable settings

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 into the event

This. dispatchevent (events );

// Send this event

}

Public Function sendcar (){

// Declare the public delivery method;

VaR info: String = "millions of sports cars ";

// Declare the sending information of the local variable settings

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 into the event

This. dispatchevent (events );

// Send this event

}

}

}

// 3. GIRLS:
Package {
Public class girl {
Public Function replay (Info): void {
Trace (Info );
}
// Declare the public method and make a response
}
}
// 4. Document class:
Package {

Import flash. display. Sprite;

// Import the sprite class

Public class documents extends sprite {

// Declare that the document class is extended from the sprite class

Private VaR _ BOY: boy;

// Declare the private property as the boy type

Private VaR _ GIRL: girl;

// Declare the private property as the girl type

Public Function documents (){

// Constructor

_ Boy = new boy;

// Create a boy instance

_ Girl = new girl;

// Create a girl instance

_ Boy. addeventlistener (customevent. sendflower, _ Hand );

// Add a customevent. sendflower event listener for the boy

_ Boy. addeventlistener (customevent. sendcar, _ Hand );

// Add a customevent. sendcar event listener for the boy

_ Boy. sendcar ();

// Call the boy's delivery method. You can try again to call the boy's sendflower

// Try the method to see if the result is different.

}

Private function _ Hand (E: customevent): void {

// Declare the event Processor

_ Girl. Replay ("I received a handsome guy who sent me:" + e.info );

// Call the girl's replay method.

}

}

}

 

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.