搞半天,其實也不是很清楚as3的事件程序,不過小試出一些心得,其實跟as2有一些類似,雖然不是還不是很了解其羅輯,不過可以試試。
以下是一個同類別物件之間的訊息廣播溝通
1、as3亦是利用EventDispatcher傳送事件,但可以直接透過EventDispatcher類別定義的物件來做到事件的控管
2、所有的displayObject都是繼承至EventDispatcher,所以其實所有的顯示物件都可以拿來做管理事件的物件
3、與as2不同,不能再用dispatchEvent({type:"type",Target:this})這個方法廣播訊息,而改dispatchEvent(event),可以自定義event物件夾帶相關的參數、訊息等等
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;
import flash.events.Event;
public class ball extends Sprite
{
static public var dispatcher:EventDispatcher=new EventDispatcher();
public function ball(Name:String)
{
this.name=Name;
graphics.beginFill(0xeeeeee);
graphics.drawRect(0,0,50,50);
graphics.endFill();
addEventListener(MouseEvent.CLICK, mouse_click);
dispatcher.addEventListener("msc", msc);
}
public function mouse_click(e:MouseEvent)
{
trace(this.name+" : "+e.type+" from "+e.target.name);
//trace(this);
var myCustomEvent=new CustomEvent("msc",this)
dispatcher.dispatchEvent(myCustomEvent);
//dispatcher.doAction(this);
}
public function msc(e:CustomEvent)
{
//trace(this.name+" listen a event("+e.type+") from "+e);
//trace(e.from.name);
trace(this.name + " listen a event("+e.type+") from "+e.target+" by "+e.from.name);
}
}
}
import flash.events.Event;
class CustomEvent extends Event
{
public var from:Object;
public function CustomEvent(type:String,o:Object)
{
from=o;
trace("from is "+from);
super(type);
}
}
沒有留言:
張貼留言