O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Compile-Time Type-Checking for Runtime-Loaded Assets

In the earlier section "Accessing the Loaded Asset," we learned that the Loader class's instance variable content refers to an object representing a loaded asset. We also learned that—depending on which type of asset was loaded—content might refer to an instance of either the Bitmap class or a .swf file's main class. Instances of those disparate classes can legally be assigned to content because its datatype is DisplayObject, and both the Bitmap class and all .swf file main classes inherit from DisplayObject. As a result, any object assigned to content can be operated on using the variables and methods of DisplayObject but cannot be operated on using the more specific variables and methods of either the Bitmap class or a .swf 's main class.

For example, the following code legally accesses the DisplayObject class's instance variable width on an object referenced via content:

// DisplayObject defines width, so no error
loader.content.width

The following code similarly attempts to access the Bitmap class's instance variable bitmapData on an object referenced via content. But this time, the code causes a compiler error because the DisplayObject class does not define the bitmapData variable.

ERROR: "Access of possibly undefined property bitmapData through a
        reference with static type flash.display:DisplayObject."

loader.content.bitmapData.getPixel(0, 0)

To avoid compile-time errors when referencing Bitmap methods and variables through ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required