-
Notifications
You must be signed in to change notification settings - Fork 203
Hello World
millermedeiros edited this page Apr 9, 2012
·
3 revisions
In this code example, Radio broadcasts a String to MessageListener, using a messageChanged Signal.
This code can be compiled as an application with mxmlc or as a document class in Flash Professional.
package
{
import org.osflash.signals.Signal;
import flash.display.Sprite;
public class SignalsHelloWorld extends Sprite
{
private var radio:Radio;
private var messageListener:MessageListener;
public function SignalsHelloWorld()
{
radio = new Radio();
messageListener = new MessageListener();
// Hook the listener up to the Radio's Signal.
radio.messageChanged.add(messageListener.onMessageChanged);
// Change the message, dispatching it to listeners.
radio.message = 'Hello World';
}
}
}
////////// PRIVATE CLASSES //////////
import org.osflash.signals.Signal;
class Radio
{
// This is the public API to which listeners subscribe.
public var messageChanged:Signal;
private var _message:String;
public function Radio()
{
// Create a Signal that dispatches a String.
messageChanged = new Signal(String);
}
public function get message():String { return _message; }
// The setter uses the Signal to dispatch the changed value.
public function set message(value:String):void
{
_message = value;
messageChanged.dispatch(_message);
}
}
class MessageListener
{
// Receive the value dispatched from the Signal.
// This is like an event handler but without the Event wrapper.
public function onMessageChanged(message:String):void
{
trace('heard message: ' + message);
}
}