ActionScript 2.0 runs on the same display model as ActionScript 1.0: timelines of frames, movie clips nested in movie clips at numbered depths, and levels above them. What it adds is the ability to give a library symbol a class of its own in a class file, plus the new display features of Flash Players 7 and 8.
The timeline model
- A timeline plays from frame 1 at the movie’s frame rate and loops unless
stop()is called. Frame scripts run each time their frame is entered. - Frames are reached by number or label:
gotoAndPlay("intro"),gotoAndStop(10),nextFrame(),prevFrame(). Scenes are joined into one timeline in the SWF. - Clips have the properties of ActionScript 1.0:
_x,_y,_xscale,_yscale,_width,_height,_rotation,_alpha,_visible,_name,_currentframe,_totalframes,_framesloaded,_xmouse,_ymouse,_parent,_target,_url,_droptarget, and from Flash Player 6enabled,useHandCursor,hitArea,tabEnabled,tabIndex,focusEnabledandtrackAsMenu. - Flash Player 7 adds
_lockrootandmenu, a clip’s own context menu.
Symbols with classes
In a symbol’s linkage properties the author ticks Export for ActionScript, gives an
identifier, and enters the full name of an ActionScript 2.0 class, which normally extends
MovieClip. Every instance of the symbol, whether placed in the FLA or made
by code, is then an instance of that class: its constructor runs, its methods and event
methods apply, and the compiler checks code that uses it with that type.
// Symbol "Ball" in the library: linkage identifier "Ball",
// ActionScript 2.0 class com.example.Ball
class com.example.Ball extends MovieClip {
public var speed:Number; // given by the init object
public function onEnterFrame():Void {
_y += speed;
if (_y > Stage.height) removeMovieClip();
}
}
// in a frame script
var b:com.example.Ball = com.example.Ball(
this.attachMovie("Ball", "ball" + n, this.getNextHighestDepth(), {speed: 3, _x: 100}));
The compiler turns the linkage into a call to Object.registerClass for
the symbol, so this is the ActionScript 1.0 mechanism with checked types. Export in
First Frame, on by default, puts the symbol in frame 1 so that it can be attached from
the start. Instances inside the symbol, such as a text field label_txt, are
declared as variables of the class so that the compiler knows their types.
Making clips and managing depths
| Method | Does | Player |
|---|---|---|
attachMovie(id, name, depth, [initObject]) | Places a new instance of a linked symbol and returns it; the init object’s properties are set before the constructor runs. | 5, 6 |
createEmptyMovieClip(name, depth) | Makes an empty clip and returns it. | 6 |
createTextField(name, depth, x, y, w, h) | Makes a text field; returns it from player 8. | 6 |
duplicateMovieClip(name, depth, [initObject]) | Copies a clip; the init object from player 6. | 5, 6 |
getNextHighestDepth() | The depth above every clip in this one, 0 or more. | 7 |
getInstanceAtDepth(depth) | The clip at a depth, if any. | 7 |
getDepth(), swapDepths(depthOrClip) | Read or change a clip’s depth. | 6, 5 |
removeMovieClip() | Removes a clip at a depth of 0 or more. | 5 |
Depths placed in the FLA start at −16383; code uses 0 to 1048575. The version 2
components’ DepthManager keeps the highest depth, 1048575, and the lowest for itself,
so with components on a timeline getNextHighestDepth can give a depth
above the range that removeMovieClip accepts.
Loading
loadMovie and loadMovieNum load SWF files and JPEG images,
and from Flash Player 8 GIF and PNG images and progressive JPEGs. Flash Player 7’s
MovieClipLoader does the same with listener events for start, progress,
completion and errors. onLoadInit is the point at which the loaded movie’s
first frame has run and its contents can be used. Setting _lockroot on
the target keeps _root in the loaded movie pointing at itself.
var loader:MovieClipLoader = new MovieClipLoader();
var watcher:Object = {};
watcher.onLoadProgress = function (t:MovieClip, done:Number, total:Number):Void {
bar_mc._xscale = 100 * done / total;
};
watcher.onLoadInit = function (t:MovieClip):Void {
t._x = 20;
};
loader.addListener(watcher);
loader.loadClip("photo.jpg", this.createEmptyMovieClip("photo", 1));
Drawing
The drawing methods of Flash Player 6, lineStyle, beginFill,
beginGradientFill, moveTo, lineTo,
curveTo, endFill and clear, gain in Flash
Player 8 more line options (pixel hinting, scaling, caps, joints and mitre limit),
gradient spread and interpolation, lineGradientStyle and
beginBitmapFill, which fills with a BitmapData.
Flash Player 8 display features
| Member | Does |
|---|---|
filters | An array of flash.filters objects, such as drop shadows, blurs and glows, applied to the clip. The array is copied on reading and writing, so changes are made to a copy and assigned back. |
blendMode | How the clip mixes with what is behind it: "normal", "multiply", "screen", "add", "difference" and others. |
cacheAsBitmap | Keeps a bitmap of the clip and redraws it only when it changes, which speeds up moving complex vector art. |
opaqueBackground | Draws the clip on a solid colour. |
scrollRect | Shows only a rectangle of the clip, for fast scrolling. |
scale9Grid | Scales the clip in nine regions so that corners keep their shape. |
transform | A flash.geom.Transform with the clip’s matrix and colour transform. |
attachBitmap(bitmapData, depth) | Shows a BitmapData inside the clip. |
getRect(target) | The clip’s bounds without its strokes. |