Public Function showtimer (){
Stage. scalemode = stagescalemode. no_scale;
Stage. align = stagealign. top_left;
Initmc ();
}
In as3, many new constants are added to replace strings. This brings us great convenience. For example, to limit the zoom mode of a video to a fixed sizeCodeIs
Stage. scalemode = "noscale ";
The value is a string, and there is no code prompt during input, so it is easy to lose errors (I often copy the string in the help document ). The code in as3 is:
Stage. scalemode = stagescalemode. no_scale;
The original string "noscale" is replaced by the constant stagescalemode. no_scale. In this way, you can use the code prompt to automatically complete the task, effectively avoiding errors.ProgramBug (and very convenient ). The same String constant has some event types, such as mouseevent. Click instead of "click.
Copy code The Code is as follows: Private function initmc (): void {
Showtxt = new textfield ();
Addshow (showtxt, 10, 10 );
Addlabel (setdelaylabel, 10, 40, "Delay :");
//... Other code
}
Add text box and button. Note that the text box to be referenced again must be explicitly initialized; otherwise, if this variable is referenced elsewhere, null is returned.
Copy code The Code is as follows: Private function addlabel (txt: textfield, X: uint, Y: uint, text: string): void {
TXT = new textfield ();
TXT. x = X;
TXT. Y = y;
TXT. Text = text;
Addchild (txt );
}
Everything in as3 is new. New is not enough. You must use addchild () to add it to the display list.
Copy code The Code is as follows: Private function addbtn (MC: Sprite,..., clickhanlder: function): void {
MC. mousechildren = false;
MC. Graphics. beginfill (0x000000, 0.3 );
MC. Graphics. drawrect (0, 0, W, H );
MC. buttonmode = true;
MC. addeventlistener (mouseevent. Click, clickhanlder );
Addchild (MC );
//
TXT = new textfield ();
TXT. Name = "btntext ";
MC. addchild (txt );
}
To make MC a button in as3, you must set it:
MC. buttonmode = true;
At this time, when the mouse passes through MC, it does not become a hand. The reason is that in the last line, the TXT is added to MC to display the button text, so that the target object of the mouse event is TXT rather than the expected MC. To solve this problem, add the following sentence:
MC. mousechildren = false;
To ensure that MC is the target object of the mouse event (target objects ).
All visible objects in as3 are subclasses of displayobject, while displayobject is a subclass of eventdispatcher.
Sprite → displayobjectcontainer → interactiveobject → displayobject → eventdispatcher → object
That is to say, all visible objects can be directly addeventlistener.
MC. addeventlistener (mouseevent. Click, clickhanlder );
Here, the constant mouseevent. Click is used to replace the event type "click ". This type of constant will not be repeated later.
MC. Graphics. beginfill (0x000000, 0.3 );
MC. Graphics. drawrect (0, 0, W, H );
All the drawing methods in as3 are stored in graphics. The graphics attribute of Sprite is a graphics. In addition to the basic beginfill and beginbitmapfill, new methods such as drawcircle, drawellipse, and drawrect are added, and there is no need to endlessly move ETO and lineto.
Public Function starttimer (Event: mouseevent): void {
//... Code here
}
Here is the main content: timer.
VaR delay: uint = setdelaytxt. text;
VaR repeatcount: uint = setrepeatcounttxt. text;
If (timer = NULL ){
Timer = new timer (delay, repeatcount );
}
Uint is the newly added data type of as3. it represents a 32-bit positive integer (INT represents a 32-bit signed integer ). The timer constructor accepts two parameters. delay is the number of milliseconds for the "timer" event delay, and repeatcount is the number of cycles. The default value is 0, that is, it continues until stop or reset.
Timer. addeventlistener (timerevent. Timer, timerhandler );
Timer. addeventlistener (timerevent. timer_complete, timercompletehandler );
Timer broadcasts two events. The "timer" event is broadcast every millisecond specified by delay, and the "timercomplete" event is broadcast after the repeatcount loop.
Timer. Start ();
Startbtn. getchildbyname ("btntext"). Text = "stop ";
Timer starts execution after start, and the running attribute is true. Set startbtn to "stop". Note that as3 cannot get the child of startbtn because Sprite is not a dynamic class and cannot declare its child. To get the text box in startbtn, use the getchildbyname method. Of course, you must first give the child a name:
// Function addbtn
TXT. Name = "btntext ";
the difference between stop and reset: After a reset is stopped, set the currentcount attribute to 0. You can use the final compiled SWF to understand it.
download the Flash animation from the FLA file online playback