自定义事件

//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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值