AS3 Script-Written timer effect code _flash as

Source: Internet
Author: User
Tags addchild
Public Function Showtimer () {
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;
INITMC ();
}

A lot of new constants are added to the AS3 instead of strings. This brings us a lot of convenience. For example, to limit the movie's zoom mode to a fixed size, the AS2 code is

Stage.scalemode = "Noscale";

A value is a string that, when entered, is not prompted by code, and is easily lost (I often copy the string in the Help document). And the code in the AS3 is:

Stage.scalemode = Stagescalemode.no_scale;

The original string "Noscale" is replaced by a constant stagescalemode.no_scale. This can be done automatically using code hints, effectively avoiding bug (and very convenient) bugs caused by errors. The same string constants also have some event types such as Mouseevent.click instead of "click" and so on.

Copy Code code as follows:

Private Function INITMC (): void{
Showtxt = new TextField ();
Addshow (showtxt,10,10,310,20);
AddLabel (setdelaylabel,10,40, "delay:");
.. other code
}

Add text boxes and buttons. Note that the text box that needs to be referenced again must be explicitly initialized, otherwise referencing this variable elsewhere will return null.

Copy Code code 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);
}

All the things in AS3 are new. Only new is not available, you must use AddChild () to add it to the display list.

Copy Code code 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);
}

In AS3 want MC to be a button must be set:

Mc.buttonmode = true;

At this time to see the mouse after the MC did not become a hand shape, the reason in the last line, the TXT added to the MC used to display button text, so that the object of the mouse event is txt rather than the expected MC. In order to solve this problem, we need to add one sentence:

Mc.mousechildren = false;

To ensure that the MC is the target object of the mouse event (target objects).

All the visible objects in the AS3 are subclasses of the Displayobject, and Displayobject is a subclass of Eventdispatcher.

Sprite→displayobjectcontainer→interactiveobject→displayobject→eventdispatcher→object

That is to say, all visible objects can be addeventlistener directly.

Mc.addeventlistener (Mouseevent.click,clickhanlder);

This replaces the event type "click" with a constant mouseevent.click. Such constants are not to be repeat later.

Mc.graphics.beginFill (0x000000,0.3);
Mc.graphics.drawRect (0,0,W,H);

All the drawing methods in the AS3 are in the Graphics. The graphics attribute of sprite is a graphics. In addition to the basic Beginfill, Beginbitmapfill, and so on, and added a new drawcircle, DrawEllipse, DrawRect and other methods, no longer endless moveTo, lineTo.

Public Function Starttimer (event:mouseevent): void{
... code here
}

Here's the main stuff: Timer.

var delay:uint = Setdelaytxt.text;
var repeatcount:uint = Setrepeatcounttxt.text;
if (timer = = null) {
Timer = new timer (delay,repeatcount);
}

UINT is the AS3 new data type, representing a 32-bit positive integer (int represents a 32-bit signed integer). The timer constructor accepts two parameters, delay is the number of milliseconds that the timer event is delayed, RepeatCount is the number of loops, and the default is 0, which loops until stop or reset.

Timer.addeventlistener (Timerevent.timer,timerhandler);
Timer.addeventlistener (Timerevent.timer_complete,timercompletehandler);

The timer broadcasts two events, broadcasts a "timer" event every delay the specified millisecond, and broadcasts the "Timercomplete" event after looping repeatcount times.

Timer.start ();
Startbtn.getchildbyname ("Btntext"). Text = "Stop";

The timer begins execution after start, at which point the running property is true. Set the startbtn to "stop" and note that AS3 is not able to get the STARTBTN child because Sprite is not a dynamic class and cannot declare its child. When you want to get the text box inside the STARTBTN, you need to use Getchildbyname method. Of course, first give the child a name:

function addbtn
Txt.name = "Btntext";

The last is the difference between stop and reset: Reset after the stop to set the Currentcount property to 0. It can be realized by the last SWF compiled.
FLA File download
Flash Animation
Play Online

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.