開發Flash或Flex系統時,覺得AS3內建的事件不甚好用嗎,那as3signals或許是一個不錯的解決方案。
AS3內建事件的相關評論,可參考:
as3Signals 是Robert Penner針對AS3內建的事件 (built-in events),所提出的一種新架構,該架構簡化了物件(或元件)間透過事件溝通的程序。
as3Signals概念摘錄
Signals: Think Outside the Event.
- Signals are light-weight, strongly-typed AS3 messaging tools.
- Wire your application with better APIs and less boilerplate than AS3 Events.
Concept
- A Signal is essentially a mini-dispatcher specific to one event, with its own array of listeners.
- A Signal gives an event a concrete membership in a class.
- Listeners subscribe to real objects, not to string-based channels.
- Event string constants are no longer needed.
- Signals are inspired by C# events and signals/slots in Qt.
以一個簡單範例作說明,當Radio的massage屬性被設定訊息後,Radio會dispach事件,並由BroadcastListener進行監聽(listener)與回應。
一般寫法:
RadioEvent類別:必須自訂事件
package
{
import flash.events.Event;
public class RadioEvent extends Event
{
public static const BROAD_CAST:String = "broadcast";
public var message:String;
public function RadioEvent(type:String, message:String,
bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
this.message=message;
}
override public function clone():Event
{
return new RadioEvent(this.type, this.message ,
this.bubbles, this.cancelable);
}
}
}
Radio類別:dispatch event
package
{
import flash.events.EventDispatcher;
[Event(name="broadcast",type="RadioEvent")]
public class Radio extends EventDispatcher
{
public var eventObject:RadioEvent;
private var _message:String;
public function get message():String
{
return _message;
}
public function set message(value:String):void
{
_message = value;
eventObject=new RadioEvent("broadcast", _message);
this.dispatchEvent(eventObject);
}
}
}
BroadcastListener類別:listener
package
{
public class BroadcastListener
{
public function onBroadcastHandler(event:RadioEvent):void
{
trace('Heard message: ' + event.message);
}
}
}
SignalsHelloWorld類別:設定Radio物件的message屬性
package
{
import flash.display.Sprite;
public class SignalsHelloWorld extends Sprite
{
private var radio:Radio;
private var broadcastListener:BroadcastListener;
public function SignalsHelloWorld()
{
this.radio=new Radio();
this.broadcastListener=new BroadcastListener();
radio.addEventListener(RadioEvent.BROAD_CAST,
this.broadcastListener.onBroadcastHandler);
radio.message="Hello World, Kolen!";
}
}
}
使用as3signals的寫法:
SignalsRadio類別:透過Signal類別dispatch event
package
{
import org.osflash.signals.Signal;
public class SignalsRadio
{
private var _message:String;
public var broadcast:Signal;
public function SignalsRadio()
{
broadcast = new Signal(String);
}
public function get message():String
{
return _message;
}
public function set message(value:String):void
{
_message = value;
broadcast.dispatch(_message);
}
}
}
SignalsBroadcastListener類別:listener
package
{
public class SignalsBroadcastListener
{
public function onBroadcastHandler(message:String):void
{
trace('Heard message: ' + message);
}
}
}
SignalsHelloWorld類別:設定Radio物件的message屬性
package
{
import flash.display.Sprite;
public class SignalsHelloWorld extends Sprite
{
private var radio:SignalsRadio;
private var broadcastListener:SignalsBroadcastListener;
public function SignalsHelloWorld()
{
this.radio=new SignalsRadio();
this.broadcastListener=new SignalsBroadcastListener();
radio.broadcast.add(this.broadcastListener.onBroadcastHandler);
radio.message="Hello World, Kolen!";
}
}
}
物件間的溝通是不是變簡單了呢,而本範例僅用到Signal類別,尚有DeluxeSignal、NativeSignal兩種類別,待後續再作介紹。
as3signals的學習資源