//CustomEvent .as
package
{import flash.events.Event;
/**
* ...
* @author littleJoe
*/
public class CustomEvent extends Event
{
public static const SENDMONEY:String = "sendMoney";
public static const SENDCAR:String = "sendCar";
public static const SENDFLOWER:String = "sendFlower";
public var _info:String;
public function CustomEvent(type:String, info:String)
{
super(type);
_info = info;
}
}
}
-----------------------------------------------------------------------
Boy.as
package
{
import flash.events.EventDispatcher;
import flash.events.Event;
/**
* ...
* @author littleJoe
*/
public class Boy extends EventDispatcher
{
public function Boy()
{
}
public function sendMoney():void
{
var info:String = "大把钞票";
var events:CustomEvent = new CustomEvent(CustomEvent.SENDMONEY, info);
this.dispatchEvent(events);
}
public function sendCar():void
{
var info:String = "豪车";
var events:CustomEvent = new CustomEvent(CustomEvent.SENDCAR, info);
this.dispatchEvent(events);
}
public function sendFlower():void
{
var info:String = "鲜花";
var events:CustomEvent = new CustomEvent(CustomEvent.SENDFLOWER, info);
this.dispatchEvent(events);
}
}
}
-----------------------------------------------------------------------
Girl.as
package
{
/**
* ...
* @author littleJoe
*/
public class Girl
{
public function Girl()
{
}
public function replay(info:String):void
{
trace(info);
}
}
}
-----------------------------------------------------------------------
Main.as
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author littleJoe
*/
public class Main extends Sprite
{
private var _boy:Boy;
private var _girl:Girl;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_boy = new Boy();
_girl = new Girl();
_boy.addEventListener(CustomEvent.SENDMONEY, _hand);
_boy.sendMoney();
_boy.addEventListener(CustomEvent.SENDCAR, _hand);
_boy.sendCar();
_boy.addEventListener(CustomEvent.SENDFLOWER, _hand);
_boy.sendFlower();
//通过这个函数来触发该事件,而系统自带的比如鼠标单击
//事件,则由鼠标单击时自动触发。
}
private function _hand(e:CustomEvent):void
{
_girl.replay("我收到了:"+e._info);
}
}
}
-----------------------------------------------------------------------
我收到了:大把钞票
我收到了:豪车
我收到了:鲜花
自定义的事件(CustomEvent)继承Event或者其子类,事件发送者(Boy)继承EventDispatcher