Signalsの3回目。
イベントを削除するためのメソッドとして NativeSignal
には remove, removeAll
が用意されています。
remove(listener:Function):Function
Unsubscribes a listener from the signal.removeAll():void
前回のコードをちょい変更。
import org.osflash.signals.natives.NativeSignal; import flash.display.Sprite; import flash.display.Shape; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; import flashx.textLayout.formats.TextAlign; import flash.display.DisplayObject; function createBox ():Sprite { var sp:Sprite = new Sprite(); var shape:Shape = new Shape(); shape.graphics.clear(); shape.graphics.beginFill(0x6699CC, .6); shape.graphics.drawRoundRect(-20, -20, 40, 40, 8); shape.graphics.endFill(); sp.addChild(shape); return sp; } var cancels:Array = []; function onClicked(e:MouseEvent):void { var box:Sprite = createBox(); box.x = mouseX; box.y = mouseY; addChild(box); // bind event var exit:NativeSignal = new NativeSignal(box, Event.EXIT_FRAME, Event); exit.add(onExit); var cancel:NativeSignal = new NativeSignal(box, Event.CANCEL, Event); cancel.addOnce(willCancel); // pool cancels.push({cancel:cancel, exit:exit});// Object型へ } // loop function onExit (e:Event):void { e.target.rotation += 6; } // cancel event handler of box function willCancel (e:Event):void { removeChild(e.target as DisplayObject) } // cancel event handler of button function onCancel (e:Event):void { while (cancels.length > 0) { // Object型変更に伴い変更 var targets:Object = cancels.shift(); targets.cancel.dispatch(new Event(Event.CANCEL)); targets.exit.removeAll();// ここでremove } } // MAIN function main ():void { // background var container:Sprite = new Sprite(); container.graphics.clear(); container.graphics.beginFill(0xefefef); container.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); container.graphics.endFill(); addChild(container); // stop button var button:Sprite = new Sprite(); button.graphics.clear(); button.graphics.beginFill(0x339933); button.graphics.drawRoundRect(0, 0, 100, 40, 8); button.graphics.endFill(); button.mouseEnabled = true; button.buttonMode = true; button.mouseChildren = false; button.x = stage.stageWidth - button.width - 10; button.y = stage.stageHeight - button.height - 10; // button label var tf:TextField = new TextField(); var format:TextFormat = new TextFormat("Helvetica", 14, 0xffffff, "bold"); format.align = TextAlign.CENTER; tf.defaultTextFormat = format; tf.width = 20; tf.height = 20; tf.autoSize = TextFieldAutoSize.LEFT; tf.text = "STOP"; tf.x = (button.width - tf.width) * .5; tf.y = (button.height - tf.height) * .5; addChild(button); button.addChild(tf); // bind event var cancel:NativeSignal = new NativeSignal(button, MouseEvent.CLICK, MouseEvent); cancel.add(onCancel); var clicked:NativeSignal = new NativeSignal(container, MouseEvent.CLICK, MouseEvent); clicked.add(onClicked); } main(); |
これだとNativeSignalインスタンス側からイベントハンドラを削除できるんだな。
でも、まだスッキリこない。
こんな使い方なんだろうか?
ピンバック: Tweets that mention Actionscript 3, Event処理を簡単に – Signals, dispatchとremoveAll | イナヅマtvログ -- Topsy.com