flash on 2010-2-19

by Makoto_Tanaka
EventDispatcherのテスト
dispatchEventを使うと好きなタイミングでイベントを発生させられる。
♥0 | Line 37 | Modified 2010-02-19 22:00:36 | MIT License
play

ActionScript3 source code

/**
 * Copyright Makoto_Tanaka ( http://wonderfl.net/user/Makoto_Tanaka )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/iGKh
 */

//
// EventDispatcherのテスト
// dispatchEventを使うと好きなタイミングでイベントを発生させられる。
//
package {
    import flash.display.Sprite;
    import flash.events.Event;
    
    import flash.text.TextField;
    public class FlashTest extends Sprite {
    	
        public function FlashTest() {
            var eventClass:EventClass = new EventClass();
            eventClass.addEventListener(EventClass.DO_EVENT, onEvent);
            eventClass.start();
        }
        
        // イベントを取得する
        private function onEvent(e:Event):void {
        		var color:Number = Math.random() * 10000000 % 16777215;
        		var x:Number = Math.random() * 1000 % 400;
        		var y:Number = Math.random() * 1000 % 400;
        		
        		this.graphics.beginFill(color);
        		this.graphics.drawCircle(x,y,40);
        		this.graphics.endFill();
        }
    }
}

import flash.events.EventDispatcher;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class EventClass extends EventDispatcher {
	public static const DO_EVENT:String = "do event";
	
	public function EventClass() {
	}
	
	// Timerを使って定期的にイベントを発生させる
	public function start():void {
		var timer:Timer = new Timer(1000);
		timer.addEventListener(TimerEvent.TIMER, onTimer);
		timer.start();
	}
	
	// 独自のイベントを発生させる
	private function onTimer(e:TimerEvent):void {
		this.dispatchEvent(new Event(DO_EVENT));
	}
	
}