- Timeline loops are effective but not necessarily elegant.
- In Flash 5 and later, we can use an
enterFrame clip event handler to achieve the same result as a timeline loop.
- An
enterFrame event handler causes a block of code to execute every time a frame passes in a movie.
- Let's recreate our timeline loop as an
enterFrame event loop (we'll start with a movie, event-loop.fla, that already has a ball clip on Stage):
- Select Insert>>New Symbol to create a blank movie clip symbol.
- Name the clip symbol
process.
- Return to the main movie timeline and create a layer called
processMove.
- On the
processMove layer, place an instance of the process symbol.
- Select the
process instance and attach the following code:
onClipEvent (enterFrame) {
_root.ball._x += 10;
}
- The clip event example we just saw has a drawback: there's no way to programmatically start or stop the loop once it's started.
- We want to create an event loop that can be arbitrarily started and stopped.
- To do so, we create an empty clip that contains another empty clip with an
enterFrame event loop.
- We then dynamically attach and remove the whole package whenever we want to start or stop the loop.
- Let's build a controllable event loop (
event-loop-controllable.fla). Our movie already has a ball clip ready:
- Select Insert>>New Symbol twice to create two blank movie clip symbols.
- Name one clip symbol
process, and the other eventLoop.
- In the library, select the
process clip, then select Options>>Linkage. The Symbol Linkage Properties dialog appears.
- Select Export this symbol.
- In the identifier box, type
processMoveBall and then click OK.
- On frame 1 of the
process clip, place an instance of eventLoop.
- Select the
eventLoop instance, and attach the following code to it:
onClipEvent (enterFrame) {
_root.ball._x += 10;
}
- Return to the main movie timeline and attach the following code to a "start move" button:
on (release) {
attachMovie("processMoveBall", "processMoveBall", 5000);
}
- Attach the following code to a "stop move" button:
on (release) {
removeMovieClip(processMoveBall);
}