10月 8, 2009
CASA for 2.0でEventDispatcher
EventDispatcherって便利だけど、理解するまで大変。
スマートな書き方をいろいろ探したけど、どれもややこしい。
ほんでいつも使ってるCASAのGoupLoadとかの機能って、
ちゃんとイベントの経過・終了を知らせてくれるから、
便利だなって思って、ちょっとパックって見た。
要は、CASAのEventDispatcherを承継すれば、
それっぱいカスタムイベントができるのだ!
サンプルはこんな感じ:
本体の「Main」クラスが、クラス「Event」を呼び出して、
「Event」の処理が終わったら、「Main」クラスの「$onComplete」を実行する。
ボタン「btn_1」を押したら、EventDispatcherをキャンセル!
なかなかステキ。
Main.as
import Event;
class Main{
public function Main(){
doEvent();
}
static function doEvent(){
//実行したいイベントクラス。と渡したい引数
var _event:Event = new Event("chinko");
//仕掛けのコード。"onLoadComplete"はクラスに存在する任意の関数名を入れる。
_event.addEventObserver( Main , _event.EVENT_LOAD_COMPLETE , "onLoadComplete");
//開始
_event.start();
_root.btn_1.onRelease = function(){
//上の発射コードと同じ引数で指定のイベントをキャンセル。
_event.removeEventObserver( Main , _event.EVENT_LOAD_COMPLETE , "onLoadComplete1");
//無差別で全イベントキャンセル
//_event.removeAllEventObservers();
}
}
public static function onLoadComplete( sender:Event , Percent, ushi ){
trace("4_終わり_"+Percent+"__"+ushi);
}
}
Event.as
import org.casaframework.event.EventDispatcher;
class Event extends EventDispatcher {
public static var EVENT_LOAD_COMPLETE:String = 'onLoadComplete';
private var $data:String;
static var loc:Object;
public function Event(todata:String) {
super();
loc = this;
this.$data = todata;
}
public function start():Void {
//イベント開始。
//空のmc作ってonEnterFrame、カウント開始
//カウントが20になったらイベント終了へ
var Dami = _root.createEmptyMovieClip( "Dami" , _root.getNextHighestDepth() );
var Hen:Number = 0;
Dami.onEnterFrame = function(){
Hen ++;
trace(Hen);
if( Hen == 20 ){
delete this.onEnterFrame;
trace("---------- End onEnterFrame ----------");
loc.$onComplete();
}
}
}
private function $onComplete():Void {
trace("Event完了、データを返す"+$data);
this.dispatchEvent( Event.EVENT_LOAD_COMPLETE , this , $data , "jiji" );
}
}
I see a lot of interesting content on your website.
You have to spend a lot of time writing, i know how to save you a lot of work, there is a tool that creates unique, google friendly articles in couple of minutes,
just type in google – k2 unlimited content
I read a lot of interesting content here. Probably you spend a
lot of time writing, i know how to save you a lot of work, there is an online tool that creates high quality,
google friendly articles in minutes, just type in google – laranitas free content source