Flash MX Prototyping Basics - Dealing with Time
(Page 3 of 4 )
When dealing with prototypes, you will often want actions to take place after an event or amount of time has passed. Let's deal with both, starting with events. The example uses a movie that reacts to the mouse being over it or not. To be specific, the movie plays forward when the mouse is not over it. When the mouse is moved over the movie, it plays in reverse.
MovieClip.prototype.alternateDirection = function() {
// Set the initial direction to play forward
this.playForward = true;
// Set mouse events up to control the play direction
this.onRollOver = function() {
this.playForward = false;
};
this.onRollOut = function() {
this.playForward = true;
};
this.onEnterFrame = function() {
if (this.playForward) {
this._currentframe == this._totalframes ? this.stop() : this.gotoAndStop(this.nextFrame());
} else {
this._currentframe == 1 ? this.stop() : this.gotoAndStop(this.prevFrame());
}
};
};
// Set the prototype off
myMovie.alternateDirection();
In this prototype an internal variable is set which controls the direction the movie plays. If you look toward the beginning of the code, you will see the onRollOver and onRollOut events being attached to the movie. These are a couple of the mouse events that you can link to your movie clips. Notice how they look very similar to prototypes? Using the same prototype you can see an example of time control via the onEnterFrame event handler. This runs continually throughout the movies life span or until the handler is deleted. All that happens here is that every frame the movie checks whether the playForward variable is true or false. You may be wondering what the strange notation is within the if and else clauses. It's another way of writing conditional statements; here's a translation:
this._currentframe == this._totalframes ? this.stop() : this.gotoAndStop(this.nextFrame());
if ( this movies current frame = the last frame ) stop it otherwise goto the next frame and stop.
Next: Self Containment >>
More Flash Articles
More By David Millinton