clamp
clamp

Reputation: 34016

Actionscript: how to pass objects to a loader event

What is the recommended way of passing objects into the eventhandlers of a Loader?

        var l:Loader = new Loader();
        var o:Object = new Object();
        l.tag = o; // i imagine something like this
        l.contentLoaderInfo.addEventListener(Event.COMPLETE, splashCompleted);
        l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, splashIoerror);         
        l.load(new URLRequest(path));


        function splashCompleted(e:Event):void
        {
            // here i want to access the object o
        }

Upvotes: 0

Views: 117

Answers (2)

skozin
skozin

Reputation: 3899

Why would you need to do that? If there are many loaders and you want to associate some data with each of them, it would be better to write either subclass or wrapper class to store your data. For example, let's consider subclass:

public class TaggedLoader extends Loader
{
    public var tag:Object;
}

Using TaggedLoader instead of Loader you can easily access data associated with the loader object:

var l:Loader = new TaggedLoader();
var o:Object = new Object();

l.tag = o;
l.contentLoaderInfo.addEventListener( Event.COMPLETE, splashCompleted );
l.load( new URLRequest( path ) );

function splashCompleted( e:Event ):void
{
    var taggedLoader:TaggedLoader = ( e.currentTarget as LoaderInfo ).loader as TaggedLoader;
    var tag:Object = taggedLoader.tag;
}

Also it is possible to use maps (Object or Dictionary), but maps are worse in terms of performance.

Upvotes: 2

poke
poke

Reputation: 387705

You can access the Loader from inside the event handler using this:

( e.currentTarget as LoaderInfo ).loader

Upvotes: 0

Related Questions