- Inside the Start File
- Putting Together the Pieces
- Scripting the Animation Controller
- On Every Frame: The Animation Script
- Mission Accomplished
On Every Frame: The Animation Script
The script is almost completed. The final section uses an onClipEvent(enterFrame) handler and, therefore, runs multiple times every second in accordance with the movie's frame rate. By triggering a set of instructions on each frame tick, a smooth animation effect is created.
Cut and paste the following code into the Actions panel, inserting it below the script from the previous step:
onClipEvent(enterFrame) { // Scroll Foreground ForegroundL._x -= (ScrollSpeed * ForegroundFactor); ForegroundR._x = ForegroundL._x + ForegroundL._width; if (ForegroundL._x > Width) { SwapForegrounds(); ForegroundL._x = ForegroundR._x - ForegroundR._width; } else if (ForegroundR._x < 0) { SwapForegrounds(); } // Scroll Midground MidgroundL._x -= (ScrollSpeed * MidgroundFactor); MidgroundR._x = MidgroundL._x + MidgroundL._width; if (MidgroundL._x > Width) { SwapMidgrounds(); MidgroundL._x = MidgroundR._x - MidgroundR._width; } else if (MidgroundR._x < 0) { SwapMidgrounds(); } // Scroll Background BackgroundL._x -= (ScrollSpeed * BackgroundFactor); BackgroundR._x = BackgroundL._x + BackgroundL._width; if (BackgroundL._x > Width) { SwapBackgrounds(); BackgroundL._x = BackgroundR._x - BackgroundR._width; } else if (BackgroundR._x < 0) { SwapBackgrounds(); } }
The scrolling tasks here are broken up into three similar code blocks, one for each layer. The first two lines of every block adjust the location of the relevant movie clips, based on the current scroll speed and the multiplication factor for that particular level. For instance, if the scroll speed is 5 and the Foregroundfactor is 2, then the foreground will be scrolled 10 pixels.
NOTE
Only the leftmost movie clips in each layer are positioned based on the scroll speed x factor formula. The other movie clip is positioned relative to the first so that it always stays perfectly aligned.
After the layers are scrolled, a conditional if...else statement is used to check whether it's time for the pieces in each layer to be switched. When the leftmost or rightmost movie clip looks like it's running out of room, the pointers are swapped and the images are shuffled to maintain scrolling continuity.