Run-time instances
Timing
In our examples we repeatedly clicked the button to generate each instance. Is it possible to make it automatically generated? You might being thinking about using a loop for example for loop which will make 100 instances of the symbol. It's a possible way, but… loop will be executed momentarily and for the user all instances will appear at once… Also it's possible "to cheat" and create a supplementary movie clip with timeline of one second, make it playing and from it's frame call the code…. But it's too awkward…
Still it is quite achievable to make instance appear with a delay. Actually any code execution may be processed in Flash repeatedly with some certain time interval.
We will make a small example now. Let's call it "caterpillar"
- Create a movie symbol of caterpillar segment. We've done this animation. Each segment will appear, grow and rise.
- In the frame of the main timeline enter the following code
function doIt() { thenumber ++; thex+= 15; thedepth ++; attachMovie("caterpillar", "caterpillar" + thenumber ,thedepth); setProperty("caterpillar" + thenumber,_y,100); setProperty("caterpillar" + thenumber,_x,thex); removeMovieClip("caterpillar" +(thenumber-15)); if (thex>550) { thex=0; } } var breed; breed = setInterval( doIt, 500);Most of the code is written in the function form. We called the function doIt().
Inside the function we firstly increment variables which keep the number, which is later is used in the instance name, depth and increased x position variable.
After that we created the instance of caterpillar segment by attaching.movie.
The next step to set positioning properties to the newly created segment.
There is a condition which checks if caterpillar reaches end of the stage. X position then will be returned to the left side of the screen.
And at last the segment which was created 10 steps before is removed.
After the function is closed with curly brace there is a very important piece of the code. The variable breed is assigned the setInterval() action, which takes as parameters name of the function to execute and interval of 500 milliseconds. Remember this clause – it will help you to animate your movies in the future.
- Test the movie – caterpillar should move from left to right. New segments "grow" from the head and disappear at the tail.