To fade an object by using the setInterval() function:
1.
Create a new Flash document called fade2.fla.
2.
Draw some graphics on the Stage, or import an image to the Stage (File > Import > Import
to Stage).
3.
Select the content on the Stage and select Modify > Convert to Symbol.
4.
Select the Movie clip option and click OK to create the symbol.
5.
Select the movie clip instance on the Stage and type img1_mc in the Instance Name text
box in the Property inspector.
6.
Select Frame 1 of the Timeline and add the following code to the Actions panel:
var alpha_interval:Number = setInterval(fadeImage, 50, img1_mc);
function fadeImage(target_mc:MovieClip):Void {
target_mc._alpha -= 5;
if (target_mc._alpha <= 0) {
target_mc._visible = false;
clearInterval(alpha_interval);
}
}
The
setInterval()
handler, because the
code should call a particular function. In this code example, the user-defined
function is called every 50 milliseconds (20 times per second). The
fadeImage()
function decrements the value of the current movie clip's
fadeImage()
When the
_alpha
function to stop executing.
fadeImage()
7.
Select Control > Test Movie to test the document.
The movie clip you added to the Stage slowly fades out.
For more information on user-defined functions, see
on page
212. For more information on the
onEnterFrame (MovieClip.onEnterFrame handler)
Reference. For more information on the
in the ActionScript 2.0 Language Reference.
function
For an example of scripted animation in Flash, you can find a sample source file,
animation.fla, in the Samples folder on your hard disk.
In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Animation.
On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Animation.
function behaves slightly differently than the
function tells Flash precisely how frequently the
setInterval()
value is equal to or less than 0, the interval is cleared, which causes the
setInterval()
"Defining global and timeline functions"
event handler, see
onEnterFrame
in the ActionScript 2.0 Language
function, see
Scripting animation with ActionScript 2.0
event
onEnterFrame
property.
_alpha
setInterval
473
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?
Questions and answers