基础篇:事件的发送和处理
在ActionScript3.0中,Event类属性有:type、cancelable、target、currentTarget、eventphase、bubles
Event方法有:clone()、toString()、stopPropogation()、stopImmediateProgation()、preventDefault()、isDefaultPrevented()和formatToString()
管理事件监听器
注册事件监听器:zc.addEventListener(MouseEvent.CLICK,onHandler)
删除事件监听器:sc.addEventListener(MouseEvent.CLICK,onHandler)
检查事件监听器:jc.hasEventListener(MouseEvent.CLICK)
所有的事件都属于flash.events包内。常用的有鼠标事件(MouseEvent),键盘事件(KeyboardEvent),时间事件(TimerEvent),帧循环(ENTER_FRAME)事件。
以下做了几个简单的实例:
var zc:Sprite=new Sprite(); zc.graphics.beginFill(0xff0000); zc.graphics.drawRect(0,0,100,20); zc.graphics.endFill(); addChild(zc); zc.x=275 zc.y=200 zc.doubleClickEnabled = true var lobel:TextField = new TextField(); lobel.text='双击按钮'; lobel.doubleClickEnabled=true; zc.addChild(lobel); zc.addEventListener(MouseEvent.DOUBLE_CLICK,onHandler); function onHandler(e:MouseEvent):void { lobel.text="你双击了按钮"; }
//创建一个可以拖动的显示对象
var zc:Sprite=new Sprite();
zc.graphics.beginFill(0xff0000);
zc.graphics.drawCircle(100,100,20);
zc.graphics.endFill();
addChild(zc);
zc.buttonMode=true;
zc.addEventListener(MouseEvent.MOUSE_DOWN,onHandler);
zc.addEventListener(MouseEvent.MOUSE_UP,onrelease);
function onHandler(e:MouseEvent):void {
e.target.startDrag();}
function onrelease(e:MouseEvent):void {
e.target.stopDrag();
}
//自定义鼠标的样式
var zc:Sprite=new Sprite();
zc.graphics.lineStyle(2,0x00FF00,2)
zc.graphics.moveTo(100,100)
zc.graphics.lineTo(145,124)
zc.graphics.lineTo(127,148)
zc.graphics.lineTo(100,113)
zc.graphics.lineTo(100,100)
addChild(zc);
stage.addEventListener(MouseEvent.MOUSE_MOVE,Move);
function Move(evt:MouseEvent):void {
zc.x=evt.stageX
zc.y=evt.stageY
evt.updateAfterEvent()
Mouse.hide()
}