Loading with AS3 Signals – Quick Tip

This post addresses people who want to replace events with Signals. I had some problems figuring out how to do it so I want to share.

In my latest project I am making heavy use of Robert Penner’s AS3 Signals framework which is an alternate approach to Flashs’ built in events. Signals offer some major advantages and I want to demonstrate one of them for using Signals when loading files with ActionScript.

If you use events your code always happen to be cluttered up with things like removeEventListener(…), you know what I mean. Signals offer the advantage to remove a signal directly after it has been called the first time. This sounds like perfect for replacing loader events like complete, ioerror, open etc.

So let’s look at the code, loading with Signals. Should be quite self explaining though.

private var loadedSignal:NativeSignal;
private var progressSignal:NativeSignal;
private var faultSignal:NativeSignal;

public function Loading()
{
	var request:URLRequest = new URLRequest("AddToStage.swf");
	var loader:Loader = new Loader();
	var signalTarget:IEventDispatcher = loader.contentLoaderInfo;

	loadedSignal = new NativeSignal(signalTarget, Event.COMPLETE, Event);
	loadedSignal.addOnce(onLoaded);

	progressSignal = new NativeSignal(signalTarget, ProgressEvent.PROGRESS, ProgressEvent);
	progressSignal.add(onProgress);

	faultSignal = new NativeSignal(signalTarget, IOErrorEvent.IO_ERROR, IOErrorEvent);
	faultSignal.addOnce(onFault);

	loader.load(request);
}

private function onFault(event:IOErrorEvent):void
{
	progressSignal.removeAll();
	trace("Something went wrong!");
}

private function onProgress(event:ProgressEvent):void
{
	trace((event.bytesLoaded / event.bytesTotal) * 100);
}

private function onLoaded(event:Event):void
{
	progressSignal.removeAll();
	addChild(event.target.content);
}

The biggest advantage in using Signals for this is the addOnce method. This method registers a listener for a signal and the first time that signal fires, it is automatically being removed. So no more removeEventListener stuff. The only signal that needs to be removed is the progress signal. This one has to use the add method because it will be fires multiple times to display the loading progress. Simply call the removeAll method on it in the onLoaded handler and the listener is gone.

Conclusion:

This is just a quick tip for those who want to start using Signals as replacement for events. Signals can do much more cool stuff so make sure you watch this video tutorial and you can get started with Signals right away. Download Signals swc file.

Tags: ,

9 Responses to “Loading with AS3 Signals – Quick Tip”

  1. Social comments and analytics for this post…

    This post was mentioned on Twitter by aidentailor: Just blogged: Loading with AS3 Signals – Quick Tip – http://tinyurl.com/ye8bwuj...

  2. Christoffer Enedahl says:

    I’d to a progressSignal.removeAll(); in the onFault handler aswell.

  3. Valentin says:

    And this approach helps how? You don’t like removeEventListener functions but adding another boilerplate code.

    • Aiden Tailor says:

      I am working with Signals really intensively and this post addresses people who want to replace regular events using Signals’ capabilities. The main reason why I myself use this approach is because of the addOnce method which cleans up my code and likely speeds it up. I do not claim that this approach helps. It is just a matter of liking.

  4. godstroke says:

    thanks, been helpful.

  5. Aristphrenia says:

    Honestly I combine Signals and Native Events – it is just so much faster to add a single addEventListener(Event.ADDED_TO_Stage etc, etc than the two step process of Native Signals.

  6. AS3-Signals is great.

    Here is a slideshow, GIT Code Sample, and screencast video about the fantastic AS3-Signals.

    http://www.rivellomultimediaconsulting.com/as3-signals-introduction/

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>