<
Windows Vista で、swf をそのまま開いて、外部ファイルを読み込むときの注意事項
| index |
Flash Develop で、パブリッシュするFlashのバージョンを選ぶ方法>
2009/06/07: IEventDispatcherの注意点
カテゴリ: FLASH (AS3)
投稿者: terada
カスタムクラスでイベントの発行を行いたい、でもすでに他のクラスを継承しちゃってる場合、AS3は多重継承できないのでIEventDispatcherインターフェースを実装するんですよね。リファレンスをみるとこんなサンプルがあります。
元のサンプルコード
でも、これってよく見るとDecoratedDispatcherクラスが別のクラスを何も継承してないのです!例えば、DecoratedDispatcherのスーパークラスをSpriteとかにした場合、下の赤字の部分を修正する必要があります。
サンプルコード修正
リファレンスのサンプルがよくない気がします。
オーバーライドのとこはエラーが出るからすぐわかるんですが、引数のthisをはずすところ、気付かずに結構はまってしまいました・・・。元のままではイベントが発行されないので、気を付けましょう。
トラックバックURL
元のサンプルコード
package {
import flash.events.Event;
import flash.display.Sprite;
public class IEventDispatcherExample extends Sprite {
public function IEventDispatcherExample() {
var decorDispatcher:DecoratedDispatcher = new DecoratedDispatcher();
decorDispatcher.addEventListener("doSomething", didSomething);
decorDispatcher.dispatchEvent(new Event("doSomething"));
}
public function didSomething(evt:Event):void {
trace(">> didSomething");
}
}
}
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;
class DecoratedDispatcher implements IEventDispatcher {
private var dispatcher:EventDispatcher;
public function DecoratedDispatcher() {
dispatcher = new EventDispatcher(this);
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean{
return dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean{
return dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
でも、これってよく見るとDecoratedDispatcherクラスが別のクラスを何も継承してないのです!例えば、DecoratedDispatcherのスーパークラスをSpriteとかにした場合、下の赤字の部分を修正する必要があります。
サンプルコード修正
package {
import flash.events.Event;
import flash.display.Sprite;
public class IEventDispatcherExample extends Sprite {
public function IEventDispatcherExample() {
var decorDispatcher:DecoratedDispatcher = new DecoratedDispatcher();
decorDispatcher.addEventListener("doSomething", didSomething);
decorDispatcher.dispatchEvent(new Event("doSomething"));
}
public function didSomething(evt:Event):void {
trace(">> didSomething");
}
}
}
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.Sprite;
class DecoratedDispatcher extends Sprite implements IEventDispatcher {
private var dispatcher:EventDispatcher;
public function DecoratedDispatcher() {
dispatcher = new EventDispatcher(); // 引数のthisを消す
}
public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public override function dispatchEvent(evt:Event):Boolean{
return dispatcher.dispatchEvent(evt);
}
public override function hasEventListener(type:String):Boolean{
return dispatcher.hasEventListener(type);
}
public override function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
dispatcher.removeEventListener(type, listener, useCapture);
}
public override function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
リファレンスのサンプルがよくない気がします。
オーバーライドのとこはエラーが出るからすぐわかるんですが、引数のthisをはずすところ、気付かずに結構はまってしまいました・・・。元のままではイベントが発行されないので、気を付けましょう。