Moving from ActionScript 2.0 to ActionScript 3.0
Moving from ActionScript 2.0 to ActionScript 3.0
Upgrading ActionScript 2.0 code to ActionScript 3.0 entails some mandatory changes to the source code. Typically the required changes are minimal to make an ActionScript 2.0 program compile without errors as an ActionScript 3.0 program. However, substantial changes may be required in order to take full advantage of the new Flash Player API. The following sections describe the changes involved in upgrading ImageViewer version 1, version 2, and version 3 to ActionScript 3.0.
Changes to take1port
The class take1port.ImageViewer includes the minimum amount of changes required to make the ActionScript 2.0 ImageViewer class compile and run as an ActionScript 3.0 class. Table 2 details these changes.
Change | Description |
---|---|
Added package statement | All classes live in packages now. |
Added import statements | Classes in the Flash Player API must be imported before use. |
Added public modifier to class |
Classes have visibility now. |
Changed constructor depth parameter datatype from Number to int |
int allows only integers (32-bit); it throws an error when a float is used. This also results in a performance gain. |
Changed container_mc property to imgLoader |
Loader instance is used instead of MovieClip instance to load the image, so a name and type change was required. |
replaced target.createEmptyMovieClip() with two statements: new Loader() and target.addChildAt() |
All objects on the display list are created through new ClassName() rather than through helper methods such as createEmptyMovieClip() and createTextField() ; objects are added to the display list through addChild() or addChildAt() . |
replaced loadMovie(URL) with Loader.load(URLRequest(URL)) |
Loading is a generic activity now. All graphics and SWF files are loaded through Loader . All other data is loaded via URLLoader . |
Changes to take1rewrite
The class take1rewrite.ImageViewer includes the changes required to modernize the code to take full advantage of the new Flash Player 9 API. Table 3 details these changes.
Change | Description |
---|---|
Changed import flash.display.MovieClip to import flash.display.Sprite |
MovieClip is no longer required. This version uses Sprite, which is a simpler display object than MovieClip (like a clip without Timeline). |
Added ability for ImageViewer to extend Sprite | As a Sprite subclass, instances of ImageViewer can be added to the display list directly. |
Removed target, depth parameters from constructor | ImageViewer is now an independent display object, not tied to a particular parent. External code can add it to or remove it from any other display object. Reparenting of objects in the display list adds flexibility |
Changed target.addChildAt(imgLoader) to addChild(imgLoader) |
imgLoader is now a child of ImageViewer, not a target. This keeps the entire package self-contained, not coupled with an external display object. Depth has been removed; it’s no longer relevant. External code will add the ImageViewer to the depth it desires. |
Changes to ImageViewer version 2
The class take2.ImageViewer includes a few basic design improvements and changes required to modernize the original ActionScript 2.0 class ImageViewer, take2. Table 4 lists these changes.
Change | Description |
---|---|
Added import flash.geom.Rectangle |
ImageViewer uses Rectangle to clip the image. |
Removed depth properties imageDepth , maskDepth , and borderDepth |
The addChild() method handles depths automatically, so asset depth class properties are not required. |
Added border property to contain border shape |
Visual assets are created, managed independently, and then parented as required; no need to insert visual assets permanently into a movie clip hierarchy. |
Added w and h properties |
Size and position are now handled in ImageViewer version 2, not in the ImageViewerDeluxe subclass. Adding these properties is a general ImageViewer API cleanup (not related to an ActionScript 3.0 feature). |
Added defaults to constuctor parameters (x:Number = 0 , and so on) |
ActionScript 3.0 throws an error when wrong number of args is supplied. Default values let external code call the constructor without arguments. This allows ImageViewer version 2 code to be used with old code that was written for ImageViewer version 1 (ImageViewer backwards compatilibity). |
Added setPosition() and setSize() methods |
This is a general ImageViewer API cleanup (not related to an ActionScript 3.0 feature). |
buildViewer no longer takes x , y , w , and h parameters |
Creation, positioning, and resizing are now separate tasks. This is a general ImageViewer API cleanup (not related to an ActionScript 3.0 feature). |
Removed the createMainContainer() method |
The image viewer container is now the ImageViewer class itself (because ImageViewer extends Sprite). |
Changed createImageClip() to createLoader() |
Loader is used instead of MovieClip. |
Removed createImageClipMask() |
In ActionScript 3.0, scrollRect can clip a display object, so no separate mask clip is required. scrollRect() is set when ImageViewer is drawn (by draw() ). |
createBorder() no longer draws border |
This is a general ImageViewer API cleanup (not related to an ActionScript 3.0). Feature border drawing now occurs in draw() . |
Added draw() method |
Image cropping and border drawing now occur in draw() , which is called by setSize() and any time the image viewer’s appearance needs to be updated. |
Removed setMask hack from loadImage() |
scrollRect can be set on Loader before loaded content arrives. |
Changes to ImageViewer version 3
The class take3.ImageViewer adds preloading support to the image viewer. Table 5 lists the significant changes from the ActionScript 2.0 counterpart ImageViewer class.
Change | Description |
---|---|
Changed createTextField() call to loadStatus_txt = new TextField(); |
TextField instances are now created with new . |
Set autoSize to TextFieldAutoSize.LEFT constant instead of string literal “left” |
Many literal string values are now stored in constants that can be type checked. |
Changed MovieClipLoader event handling methods to Loader equivalents |
Note the use of method closures that allow a function to be passed to addEventListener() while retaining its reference to the object on which it is defined. |
Changes to ImageViewer version 4
The ActionScript 3.0 version of ImageViewer version 4 is a new class with no ActionScript 2.0 equivalent. It explores one way to add drag-and-drop functionality to the image viewer widget.
Comments