Mike works as a web designer for a small agency in Seattle, Washington. He enjoys flash actionscripting and is an avid artist.
More about Mike »Forwards/Backwards Timeline
This effect in basic terms is like a yoyo. The actionscript will control the playheads direction to go in reverse should it reach the last frame. Upon doing so it will then play forward as normal in a continuous cycle, back and forwards. The beauty of it is that it can be used either on the main root timeline or for any movieclip. For this example I am using the main timeline, which I have a red ball animating left to right over 50 frames. To get this working is to simply copy the below actionscript into the first frame (not on the movieclip).
onEnterFrame = function() {
if (_currentframe == 1) {
forward = true;
} else if (_currentframe == _totalframes) {
forward = false;
}
if (forward == true) {
play();
} else {
gotoAndStop(_currentframe - 1);
}
frame = "Frame " + _currentframe;
}
Test your movie and it should be working as is with only a few lines of code. As with all tutorials on Multimedia Life, lets break it down.
onEnterFrame = function() {
...
}
We want our actionscript to execute repeatedly at the frame rate of the SWF file, so using an onEnterFrame handler will do so.
...
if (_currentframe == 1) {
forward = true;
} else if (_currentframe == _totalframes) {
forward = false;
}
...
Our first line of code is a conditional if statement. We are using the _currentframe property, which returns the current frame number of the playhead. Firstly we are checking if it equals 1, if so we set a new variable called “forward” to equal true. It will set the “forward” variable to equal false if the _currentframe is equal to the _totalframes. The _totalframes is a property similar to _currentframe only it will read how many frames are in the playhead. We could replace _totalframes with an actual frame number, but by doing it this way we can change the length of the timeline and not need to modify the actionscript. Now that we are checking wether the playhead is at the start or end of the timeline, we can use this information to control the direction of play. Therefore we need another if statement to check what the forward variable is set to.
...
if (forward == true) {
play();
} else {
gotoAndStop(_currentframe - 1);
}
...
This is fairly simple, if the forward variable equal true, play the timeline as normal, but if it doesn’t equal true, the timeline will gotoAndStop the _currentframe minus 1. Test your movie and the animation should bounce like a yoyo back and forth.
Next create a dynamic text field on the stage and give it an instance name of “frame” and copy the next line of code inside the onEnterFrame handler.
...
frame = "Frame " + _currentframe;
...
All this does is set the textfield to write “Frame “ and include the _currentframe of the timeline.


Post new comment