I’ve been playing with AS3Signals lately, and think I will be replacing flash events with it on all future projects.
It’s so simple!
So what is AS3Signals?
AS3Signals is a lightweight replacement for Flash Events written by Robert Penner.
AS3Signals aims to reduces the amount of traditional ‘boiler plate’ code required to receive and dispatch messages in Flash.
Why use AS3Signals?
Writing custom events tends to be a bit of a tedious chore on most projects, and I know many developers approach custom Events in different ways.
Some choose to create a single flexible Event with a loosely typed payload (ie. data:Object), while others like myself, like to strongly type events with value objects wherever I can (resulting in many custom event classes per project).
AS3Signals practically does away with the need for custom event classes altogether, as you now simply append any number of parameters to your signal.
The time saved not having to write all of these custom events alone is reason enough to jump into AS3Signals.
AS3Signals also brings a few nice features that events don’t have, such as “addOnce” that automatically removes the listener the first time a signal is received, “removeAll” which removes all listeners of a given signal, as well as “NativeSignal” which allows you to map traditional events (like MouseEvents) to signals. Finally there is DeluxeSignal which adds the ability to get the target object of a signal, and enables bubbling
So how do I use AS3Signals?
John Lindquist has a video tutorial that sold the idea and simplicity of AS3Signals to me, and I expect just about any other flash developer who watches it.
I’ve included my own basic demo and source code below to illustrate just how easy they are to use.
Sample
You can click on the face icon to “hit” the player. The SignalsDemo.as class listens to the hit, kill and click signals from the Player.as class and prints messages to the textfield.
SignalsDemo.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package com.pleribus.as3signalsdemo { import flash.events.MouseEvent; import flash.text.TextField; import flash.display.Sprite; /** * @author Clint Hannaford */ public class SignalsDemo extends Sprite { public var player:Player; public var textOutput:TextField; public function SignalsDemo() { addPlayer(); addTextField(); // add signal listeners player.playerHit.add(onPlayerHit); player.playerKilled.add(onPlayerKilled); player.click.add(onClickPlayer); } private function addPlayer():void{ player = new Player(); player.init(); player.x = 30; player.y = 30; player.buttonMode = true; addChild(player); } private function addTextField():void{ textOutput = new TextField(); textOutput.x = 10; textOutput.y = 100; textOutput.width = 350; textOutput.height = 200; textOutput.border = true; addChild(textOutput); } //Signal Handlers private function onClickPlayer(event:MouseEvent) : void { // Hit the player when clicked on player.hit(); } private function onPlayerKilled(message:String) : void { textOutput.appendText(message+"\n"); player.playerHit.remove(onPlayerHit); player.playerKilled.remove(onPlayerKilled); player.click.remove(onClickPlayer); this.removeChild(player); } private function onPlayerHit(message:String, health:Number) : void { textOutput.appendText(message+" - health now: "+health+"\n"); } } } |
Player.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.pleribus.as3signalsdemo { import org.osflash.signals.natives.NativeSignal; import org.osflash.signals.Signal; import flash.display.Sprite; import flash.events.MouseEvent; /** * @author Clint Hannaford */ public class Player extends Sprite { public var playerKilled:Signal; public var playerHit:Signal; public var click : NativeSignal; private var health: Number; public function Player(){ } public function init() : void { health = 10; playerKilled = new Signal(); playerHit = new Signal(); click = new NativeSignal(this, MouseEvent.CLICK, MouseEvent); with (this.graphics){ beginFill(0x000000); drawRect(0, 0, 50, 50); endFill(); beginFill(0xFFFFFF); drawRect(10, 10, 10, 10); drawRect(30, 10, 10, 10); drawRect(10, 30, 30, 10); } } public function hit():void{ health--; playerHit.dispatch("Player Hit", health); if (health <= 0){ kill(); } } private function kill():void{ playerKilled.dispatch("Player Killed"); } } } |












Simon
March 29th, 2010
Fantastic, you’ve sold me, I’m using it for a Flex AIR app i’m starting on now
saumya
March 31st, 2010
I am thinking of writing an Hello world tutorial on Signals,but your example simply the best. Still,I will try and pen down my ideas to make me understand the Signals better.
thanks for the example
saumya