View comments | RSS feed

Loading display objects

Loader objects are used to load SWF files and graphics files into an application. The Loader class is a subclass of the DisplayObjectContainer class. A Loader object can contain only one child display object in its display list--the display object representing the SWF or graphic file that it loads. When you add a Loader object to the display list, as in the following code, you also add the loaded child display object to the display list once it loads:

var pictLdr:Loader = new Loader();
var pictURL:String = "banana.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
this.addChild(pictLdr);

Once the SWF file or image is loaded, you can move the loaded display object to another display object container, such as the container DisplayObjectContainer object in this example:

import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;
var container:Sprite = new Sprite();
addChild(container);
var pictLdr:Loader = new Loader();
var pictURL:String = "banana.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded); 
function imgLoaded(event:Event):void
{
    container.addChild(pictLdr.content); 
}

Flash CS3


Comments


philggg said on Dec 5, 2007 at 6:32 PM :
The example is incorrect ..
The correct function should be:
function imgLoaded(event:Event):void
{
container.addChild(event.target.content); //THIS LINE WAS WRONG
}
vinnyguido said on Jan 9, 2008 at 9:20 AM :
The example is not wrong. Either method works for me. You can access the image from the loader's content property or the event.target's content property.
- Vinny
vonWolfenhaus said on Feb 24, 2008 at 9:30 PM :
The example is wrong only if you try putting it in a package. You can only access the image data from the loader's content property if you allow the loader to be available throughout the class.

In the example, the function imgLoaded can call upon the variable pictLoader because that variable was instantiated at a higher level (the document level as opposed to the function level).

For the example to work in a package, it should look something like this:

package
{
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*;

public class LoaderExample extends Sprite
{
var pictLdr:Loader = new Loader(); // Placed outside of any function
var container:Sprite = new Sprite(); // Same here

public function LoaderExample()
{
addChild(container);
var pictURL:String = "banana.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq); // Accessed from THIS function....
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
}

function imgLoaded(event:Event):void
{
container.addChild(pictLdr.content); // ...and THIS function as well
}
}
}

You can't call a variable that's in one function from a different function (unless you mess with the access control modifier of the variable). In other words, if you want to access a variable from multiple functions, that variable must be placed at the class level.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000171.html