[As3.0] Learn ActionScript 3.0 (10) step by step)

Source: Internet
Author: User

In the previous section, we talked about the message transmission before the Class and Class. It is actually a message sending class, and another class listens to this message. Can we transmit parameters when sending the message, of course, this is acceptable. In fact, it is very convenient to transmit parameters in 2.0 when sending messages, but in 3.0, it is a lot of trouble to write a class to inherit the event class, at the beginning of my study of 3.0, I didn't quite understand why 3.0 became complicated. So I often used the extended event class at the beginning, but later I found that as long as the program architecture is well written, this method is dispensable, but we still need to explain the use of this method today.

Let's first write a class that inherits the event:

Package net. smilecn {

Import flash. Events. event;

Public class myevent extends event {

Private VaR _ OBJECT: object;

Public Function myevent (type: String, object: Object): void {
Super (type );
_ OBJECT = object;
}

Public Function get param (): object {
Return _ object;
}
}

}

This very simple class inherits the event class. There is a super (type) in the constructor. Super means to execute the constructor of the parent class, that is to say, the construction of the execution event. type is the type of the message, that is, the name of the message. A string, object is a variable used for the parameters to be passed, and an object, we know that the object is very convenient. We can store one or more parameters. Below is a get method, using the param name to get the value of the _ OBJECT object.

Let's take a look at how to use this myevent class to modify the files in the previous section;

Myclass:

Package net. smilecn {

Import flash. display. Sprite;
Import flash. Events. mouseevent;
Import net. smilecn. myevent;

Public class myclass extends sprite {

Public Function myclass (){
Addeventlistener (mouseevent. Click, clickhnadler );
}

Private function clickhnadler (Event: mouseevent): void {
Dispatchevent (New myevent ("myclassmessage", {A: 100, B: 200 }));
}
}

}

We found that there were not many code changes, from new event to net myevent. That is to say, the previously sent message is the event that comes with the AS, and now we are sending the myevent event that we wrote ourselves, let's look at another {A: 100, B: 200} parameter. this parameter is the object we passed in. This object has two attribute values: A, with a value of 100, A name is B and the value is 200;

Let's take a look at the document class:

Package net. smilecn {

Import flash. display. Sprite;
Import net. smilecn. myevent;
Import net. smilecn. myclass;

Public class mainform extends sprite {

Private var myclass: myclass;

Public Function mainform (){
Myclass = new myclass ();
Addchild (myclass );
Myclass. addeventlistener ("myclassmessage", myclassmessagehandler );

}

Private function myclassmessagehandler (Event: myevent): void {
Trace ("you have received the message of myclass: A =" + event. Param. A + "B =" + event. Param. B );
}
}
}

The modified content in the document class is the method executed after listening to the message. We found that the parameter type is changed to myevent because we sent the myevent event, the type of the event received here is of course myevent. param is to get myeevent_object, which is the object we passed in myclass, event. param. the value of A is 100, event. param. the value of B is 200, so that we can pass the parameter.

Reprinted: http://blog.csdn.net/arrowyoung/article/details/2137606

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.