<< Chapter < Page Chapter >> Page >

Straightforward code

The code in Listing 3 is fairly straightforward. You should be able to understand it if you analyze it using the ActionScript documentation.

The Event.COMPLETE event handler

The main thing that I want to emphasize from Listing 3 is the registration of the Event.COMPLETE event handler. Note that this event handler is registered on the contentLoaderInfo property of the Loader object and not on the Loader object itself. The documentation has this to say about this property:

"Returns a LoaderInfo object corresponding to the object being loaded. LoaderInfo objects are shared between the Loader objectand the loaded content object. The LoaderInfo object supplies loading progress information and statistics about the loaded file.

Events related to the load are dispatched by the LoaderInfo object referenced by the contentLoaderInfo property of the Loaderobject. The contentLoaderInfo property is set to a valid LoaderInfo object, even before the content is loaded, so that you can add event listeners to the objectprior to the load."

The Event.COMPLETE event

The documentation has this to say about the Event.COMPLETE event:

"Dispatched when data has loaded successfully. In other words, it is dispatched when all the content has been downloaded andthe loading has finished. The complete event is always dispatched after the init event. The init event is dispatched when the object is ready to access, thoughthe content may still be downloading."

Beginning of the complete event handler

The complete event handler that is registered in Listing 3 begins in Listing 4. This handler is executed when the load process is completeand the image data is available.

Beginning of the complete event handler.

private function completeHandler(event:Event):void{ //Get, cast, and save a reference to a Bitmap object// containing the content of the image file. var original:Bitmap = Bitmap(event.target.loader.content);//Set the width and height of the VBox object based // on the size of the original bitmap.this.width = original.width + 10; this.height = 3*original.height + 12;

Get a reference to the Bitmap object

The handler begins by using the incoming reference to the Event object to execute a complex statement that ends up with a reference to the Bitmap object. However, that reference is received as type DisplayObject and must be cast to type Bitmap to be used for its intended purpose in this program. The reference is cast to type Bitmap and saved in the variable named original .

When the first statement in Listing 4 finishes executing, the variable named original refers to a Bitmap object containing the image from the image file specified in Listing 2.

Set the dimensions of the VBox object

After creating the new Bitmap object, Listing 4 uses the dimensions of the Bitmap object to set the dimensions of the VBox object, which is shown by the yellow background in Figure 1.

Can use almost any image

All of the placement information for the images shown in Figure 1 is based on the dimensions of the Bitmap object. Therefore, you should be able to substitute any JPEG, PNG, or GIF image file in place of my file so longas the name of the file matches the name and location of the file specified in Listing 2. Note however that your image file will need to be wide enough andtall enough to prevent the magenta and green color bars added to the center image in Figure 1 from extending outside the image.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask