Jump to content
Search Community

Dispatch progress

lucgeme test
Moderator Tag

Recommended Posts

Hello,

I haven't came across yet if it's possible to dispatch the progress of a loading file.  I have tried and got an error. The event.target is null !?

 Error #1009: Cannot access a property or method of a null object reference.

For example in my Main.as I have this:

preloader = new Preloader();
preloader.x = 200;
preloader.y = pStage.stageHeight/2 - _preloader.height;
addChild(_preloader);

var loader:LoadingClass = new LoadingClass ();

loader.addEventListener(LoaderEvent.PROGRESS, function(event:LoaderEvent){
									preloader .progress(event.target.progress);
									});

My LoadingClass.as is:

LoaderMax.activate([ImageLoader, SWFLoader, MP3Loader]);
queue = new LoaderMax({name: "mainQueue",
					maxConnections: 1,
					autoDispose:true,
					onProgress:progressHandler,
					onComplete: completeHandler});
queue.append(new XMLLoader("bin/xml/assets.xml", {name:"graphics", estimatedBytes:162315}));
queue.load();

private function progressHandler(event:LoaderEvent):void
{
       dispatchEvent(event);
}

private function completeHandler(event:LoaderEvent):void
{
        trace("loading complete");
}
Link to comment
Share on other sites

I made a basic example similar to yours and was able to see that the re-dispatched event did not have a target.

 

I am not an expert on the internals of the LoaderEvent or even Flash's event system, but give this a shot:

 

 

private function progressHandler(event:LoaderEvent):void
{
       dispatchEvent(event.clone() as LoaderEvent);
}

Note, as recommended many places online like: http://stackoverflow.com/a/5613784

it is necessary to override Event.clone() in custom events. LoaderEvent.as does this

 

 

public override function clone():Event{
return new LoaderEvent(this.type, _target, this.text, this.data);
}

Its my understanding that clone() should be called behind the scenes automatically so I'm not sure why I had to explicitly call it, but it seemed to solve the problem.

 

If I can get you more info on this I will.

 

 

Link to comment
Share on other sites

Yeah, this is actually a result of a workaround we had to implement in LoaderEvent to ensure that the target was correctly maintained. You see, when you dispatch an event, Flash automatically checks to see if the target is null, and if it's not, it automatically calls clone() and REPLACES the target with that object (the one that called dispatchEvent()). But it was important to us that if, for example, an ImageLoader dispatches a LoaderEvent and it bubbles up through its parent LoaderMax, the "target" should refer to that ImageLoader whereas the "currentTarget" should refer to the LoaderMax instance. That's consistent with how things work in the display list in Flash.

 

You'll notice that there's a target getter function that has been overridden in LoaderEvent so that the first time it is called, it returns null but every time thereafter it returns the correct value. This is the only way to get the correct behavior as described above. Without that, clone() would be called internally and the target would get replaced, thus it wouldn't point at the right thing. There's a note about it in the source.

 

So any of these techniques will work:

  1. As Carl prescribed, just use clone().
  2. Dispatch a new LoaderEvent like this: dispatchEvent(new LoaderEvent(e.type, e.target))
  3. Just reference the "target" again because remember, you'll only get null the FIRST time. (this isn't necessary in normal usage - it's just because you're manually passing along this event and re-dispatching it from your own class. The event.target should always be correct on events that are dispatched directly by any LoaderMax-related class, and you wouldn't get a null target)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...