How To - An Answer

The purpose of this How-To is to show how to use compiler errors and AS3 Language Reference to figure out what built-in AS3 classes should be imported into your custom AS3 class. To illustrate the process, we use an applet that has the same functionality as the applet in How To Load an Image at Runtime in AS3 and Display Load Progress. The difference is that in this example all the code is placed in an external AS3 class file.

Download

The Class File and The Answer

When you open the zip package, you will see that the fla file, htimportas3.fla, has no Timeline code. Instead, you see LoadOneImgHowTo name in the Document Class field. Indeed, the class file LoadOneImageHowTo.as appears in the same folder as the fla file. When you put your code on the Timeline, all the necessary AS3 built-in classes are automatically imported. It is not so in a custom AS3 class, a Document Class or otherwise.

Here is the code in the class LoadOneImgHowTo. Plase note all the import statements following the package wrapper. This is our focus in the present How-To. You can basically ignore the rest of the code. (For explanations of the loading process see How To Load an Image at Runtime in AS3 and Display Load Progress.)

In htimportas3.fla, we created some static text and a dynamic TextField with the instance name 'infoBox'. In this field, we display messages to the user about loading. We also created on the Stage a button with the instance name 'btnLoad'.

 

package {

 

//Any Document Class has to extend the MovieClip class or the Sprite class.

 

import flash.display.MovieClip;

 

//The next two classes have to do with infoBox,

//and the button, btnLoad, (both created on the Stage).

//In Flash CS4, they are automatically imported.

 

import flash.text.TextField;

import flash.display.SimpleButton;

 

//MouseEvent and all the subsequent classes

//have to be imported in CS3 and CS4 alike.

 

import flash.events.MouseEvent;

 

//The remaining classes are necessary for our instance of the Loader

//class to function.

 

import flash.display.Loader;

import flash.events.Event;

import flash.events.ProgressEvent;

import flash.events.IOErrorEvent;

import flash.net.URLRequest;

 

public class LoadOneImageHowTo extends MovieClip {

 

private var loader:Loader;

 

//The class constructor. The constructor will be

//evoked automatically when the movie opens.

 

public function LoadOneImageHowTo (){

loader =new Loader();

this.addChild(loader);

loader.x=65;

loader.y=50;

loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadingError);

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,updateInfo);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,doneLoad);

btnLoad.addEventListener(MouseEvent.CLICK,doLoad);

infoBox.text="Click the arrow button to load an image.";

}

 

//End of constructor.

 

private function doLoad(e:MouseEvent):void {

loader.load(new URLRequest("space2.jpg"));

infoBox.text="Loading starts...";

btnLoad.removeEventListener(MouseEvent.CLICK,doLoad);

btnLoad.visible=false;

}

 

private function updateInfo(e:ProgressEvent):void {

infoBox.text="Loading: "+String(Math.floor(e.bytesLoaded/1024))+" KB of "+String(Math.floor(e.bytesTotal/1024))+" KB.";

}

 

private function loadingError(e:IOErrorEvent):void {

infoBox.text="There has been an error loading the image. The server may be busy. Refresh the page and try again.";

}

 

private function doneLoad(e:Event):void {

loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,doneLoad);

loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,updateInfo);

loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,loadingError);

infoBox.text="";

infoBox.visible=false;

}

 

}

}

 

As you see we had to explicitly import many built-in AS3 classes in our class. To see how to find the right classes to import, download the htimportas3.zip file linked above. Open htimportas3.fla and LoadOneImgHowTo.as. Comment out one of the import statements in LoadOneImgHowTo.as, say:

//import flash.events.ProgressEvent;

So now one of the necessary AS3 classes is missing. Test htimportas3.fla. You get the following compiler error 1046:

The error tells us that the built-in AS3 class, ProgressEvent, is missing. How to find the package in which the class resides? Go to the online AS3 Language Reference: Flash CS4 Professional ActionScript 3.0 Language and Components Reference

In the lower left window under the heading 'All Classes', all buitl-in AS3 classes are listed alphabetically. Scroll to ProgressEvent and click on it. The package in which the class resides, flash.events, is displayed in the main window.

Now we know: we need the import statement:

import flash.events.ProgressEvent;

The same way you can find all the classes that you need to import and avoid importing too much with statements like import flash.display.*;.

Occasionally, you may get copiler error 1017 or 1180 instead of 1046:

1017: The definition of base class MovieClip was not found.

1180: Call to a possibly undefined method URLRequest.

You handle those the same way.

Comments

It may be useful to compare the Timeline code in How To Load an Image at Runtime in AS3 and Display Load Progress with the code in LoadOneImageHowTo.as. Right away you can see basic differences between placing your code on the Timeline and placing your code in an AS3 class file. Those differences are in form rather than substance: the package wrapper, access modifiers (private, public, etc.), to name a couple.

In the example here we used a Document Class to illustrate our point. The construct of a Document Class was introduced in Flash CS3 and it essentially allows for keeping all your code in external files. If a Document Class is assigned to a fla file, the class is instantiated by the Flash Player the moment the movie opens. (The MainTimeline is, technically speaking, an instance of the Document Class.)

The image of the outer space that you see in the applet above is courtesy of NASA: NASA multimedia collection.

Back to AS3 How To and Tips              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.