AS3 Signals – Data Payloads
Following on from my previous post in dealing with AS3 Signals, we’re going to look at how we would use Signals like custom Events and how data can be transmitted through them.
Starting with the same two files as we have used previously, in the following example you can see a document root that listens for a Signal and invokes a method of our BasicObject. This time the Signal will be carrying data that we can work with:
package { import flash.display.Sprite; /** * This is the second of three Signals tutorials. * In this example we will be looking at the basic usage * of a Signal, how it's dispatched and how we work with * the Signal when it's been received by the Listener. * * @author Anton Mills * @version 05/09/2010 */ public class Main extends Sprite { // an object private var basicObject:BasicObject; /** * constructor */ public function Main() { // create a BasicObject basicObject = new BasicObject(); // add a listener that will fire repeadly basicObject.basicSignal.add(basicSignalListener); // test the dispatch functionality basicObject.basicAction(); basicObject.basicAction(); basicObject.basicAction(); basicObject.basicAction(); } /** * a listener for the BasicObjects basicSignal * Signal events */ private function basicSignalListener(_message:String, _count:uint, _basicObject:BasicObject):void { trace(_message, _count, _basicObject); } } } |
So this isn’t a whole lot different to what we had before, the most notable difference is the basicSignalListener now has three parameters a String _message, an unsigned integer called _count and lastly a reference to a BasicObject instance called _basicObject. All we simply do is trace out the three parameters. There’s a few small changes to our BasicObject class here:
package { import org.osflash.signals.Signal; /** * This BasicObject is just that, it's here to * simply dispatch a few Signals that we'll listen * for in Main. It's also worth noting that * BasicObject doesn't need to extend EventDispatcher, * your objects can remain as light as possible. * * @author Anton Mills * @version 05/09/2010 */ public class BasicObject { // keep the signal public so you attach listeners to it externally public var basicSignal:Signal; // some local variables private var message:String; private var dispatchCount:uint; /** * constructor */ public function BasicObject() { // set up the variables message = "We have Signal"; dispatchCount = 0; // set up the signals basicSignal = new Signal(String, uint); } /** * Think of this as a method that performs * some sort of functionality. Once the * functionality is complete we will dispatch * our Signal. */ public function basicAction():void { dispatchCount++; // then dispatch basicSignal.dispatch(message, dispatchCount); } } } |
You’ll notice theres a few changes to this class, we’ve added two properties, a String called message and an unsigned integer called dispatchCount. They’re instantiated in the constructor and this time you’ll notice when we instantiate the Signal in the constructor we define the data that the Signal will carry. In this case we’ve told it the first parameter will be a String and the second parameter in an unsigned integer, adding data types to the Signal that don’t match what is expected will throw errors making it easy to locate problems.
Finally the basicAction method has been changed slightly, now each time it is invoked it increments dispatchCount and dspatches the Signal with the local properties message, dispatchCount and a reference to itself using ‘this’ to be traced out by our Listener.
Hopefully you can see at this stage just how quick and simple Signals are but also how beneficial they are in wiring your application together. Moving forward you’ll probably want to begin looking at DeluxeSignals as they allow a little more flexibility when registering Listeners as you can work with priorities using: addWithPriority(listener:Function, priority:int = 0). Next up I’ll probably be looking at how we use Signals with native events, such as MouseEvent.CLICK to make a usable application (albeit simple of course!).
anton.