- In flash 5, it wasn't easy to connect a movie clip symbol to an actionscript class.
- In flash 5, a function in a clip couldn't be executed externally until the frame after the clip appeared.
- Flash 6 adds the following tools for making movie clip subclasses:
- #initclip/#endinitclip
- Object.registerclass()
- attachMovie,duplicateMovieClip initObj
- new MovieClip()
- export frame for attachMovie() can now be set to something other than before the movie's first frame
- This is how you'd make a normal Ball class.
function Ball (theColor) {
this.setColor(theColor);
}
- But to tell Flash that Ball instances should be movie clips, we must:
- assign MovieClip as Ball's superclass
- define which libary symbol should be used to create Ball instances
- pass parameters to the Ball contstructor when creating Ball instances (via attachMovie(), duplicateMovieClip())
- Here's the revised code. You can download a working .fla here.
// The constructor takes no parameters. They are passed by
// attachMovie() and duplicateMovieClip().
function Ball () {
// this.theColor is set by the attachMovie() initObj
this.setColor(this.theColor);
}
// Set Ball superclass to MovieClip
Ball.prototype = new MovieClip();
// Associate the Ball class with a library symbol
Object.registerClass("ballSymbol", Ball);
// Create a few Ball methods
Ball.prototype.setColor = function (newColor) {
var ballColor = new Color(this);
ballColor.setRGB(newColor);
}
Ball.prototype.startMoving = function (moveAmount) {
this.onEnterFrame = function () {
this._x += moveAmount;
}
}
Ball.prototype.stopMoving = function () {
delete this.onEnterFrame;
}
- The Ball subclass is done. Now we can create instances of it:
// Make an instance. The Ball constructor is called automatically.
// The properties of our initObj (attachMovie()'s last argument)
// are set on the redBall instance before the constructor runs.
this.attachMovie("ballSymbol", "redBall", 1, { theColor: 0xFF0000 });
// Call a method.
this.redBall.startMoving(5);
// this.redBall.stopMoving()