Created
April 27, 2014 04:45
-
-
Save z-ohnami/11337782 to your computer and use it in GitHub Desktop.
Event with data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import flash.display.Sprite; | |
import flash.text.TextField; | |
import flash.events.Event; | |
import flash.events.DataEvent; | |
import flash.events.TimerEvent; | |
import flash.utils.Timer; | |
class Act111 extends Sprite | |
{ | |
private var _rect:MyRect; | |
private var _timer:Timer; | |
private var _toggle:Bool = false; | |
public function new() | |
{ | |
super(); | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(event:Event) | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
//draw rect | |
_rect = new MyRect(); | |
_rect.x = 0; | |
_rect.y = 0; | |
addChild(_rect); | |
_timer = new Timer(1000); | |
_timer.addEventListener(TimerEvent.TIMER,onTimer); | |
_timer.start(); | |
} | |
private function onTimer(event:TimerEvent):Void | |
{ | |
// _rect.dispatchEvent(new Event(MyRect.EVENT_MOVE)); | |
_rect.dispatchEvent(new DataEvent(MyRect.EVENT_MOVE,false,false,Std.string(getNextColor()))); | |
} | |
private function getNextColor():UInt | |
{ | |
var color:UInt = 0x000000; | |
if(_toggle) { | |
color = 0x0000FF; | |
_toggle = false ; | |
// trace('青色になりました。'); | |
} else { | |
color = 0xFF0000; | |
_toggle = true ; | |
// trace('赤色になりました。'); | |
} | |
return color; | |
} | |
} | |
class MyRect extends Sprite | |
{ | |
public static inline var EVENT_MOVE:String = 'event_move'; | |
private var _rect:Sprite; | |
public function new() | |
{ | |
super(); | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(event:Event) | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
//draw rect | |
_rect = new Sprite(); | |
drawRect(); | |
addChild(_rect); | |
addEventListener(EVENT_MOVE,onReceiveEvent); | |
} | |
private function onReceiveEvent(event:DataEvent):Void | |
{ | |
trace(event.data); | |
x += 10; | |
y += 10; | |
drawRect(Std.parseInt(event.data)); | |
// drawRect(cast(event.data,UInt)); | |
} | |
private function drawRect(color:UInt = 0x00FF00):Void | |
{ | |
_rect.graphics.clear(); | |
_rect.graphics.beginFill(color); | |
_rect.graphics.drawRect(0,0,100,100); | |
_rect.graphics.endFill(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment