事件流机制
首先,建立6个不同的元件,分别起元件名字为m1,m2,m3,m4,m5,m6,然后写下如下代码:
function childMCTrace(evt:MouseEvent)
{
trace("childTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function anotherMCTrace(evt:MouseEvent)
{
trace("anotherTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function parentMCTrace(evt:MouseEvent)
{
trace("parentTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function captureTrace(evt:MouseEvent)
{
trace("captureTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function stageTrace(evt:MouseEvent)
{
trace("stageTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function stageTrace1(evt:MouseEvent)
{
trace("stageTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function stageTrace2(evt:MouseEvent)
{
trace("stageTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
function stageTrace3(evt:MouseEvent)
{
trace("stageTrace",'我是:'+evt.currentTarget.name);
trace('事件所处阶段:'+evt.eventPhase);
trace('----------------------------------------------------------');
}
m1.addEventListener(MouseEvent.MOUSE_DOWN,captureTrace,true);//捕获到
m1.addEventListener(MouseEvent.MOUSE_DOWN,parentMCTrace);
m1.m2.addEventListener(MouseEvent.MOUSE_DOWN,childMCTrace);
m1.m2.m3.addEventListener(MouseEvent.MOUSE_DOWN,anotherMCTrace);
m1.m2.m3.m4.addEventListener(MouseEvent.MOUSE_DOWN,stageTrace);
m1.m2.m3.m4.m5.addEventListener(MouseEvent.MOUSE_DOWN,stageTrace1);
m1.m2.m3.m4.m5.m6.addEventListener(MouseEvent.MOUSE_DOWN,stageTrace2);
stage.addEventListener(MouseEvent.MOUSE_DOWN,stageTrace3);
(这节课可以上我们懂得事件的触发顺序,以及什么是行为事件、目标事件、冒泡事件。)
启动上机时间的小程序
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer=new Timer(50);
timer.start();
timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
function onTimerHandler(e:TimerEvent):void
{
var time:int=getTimer();
var str:String=uint(time/1000/3600)+'小时'+uint(time/1000%3600/60)+'分钟'+time/1000%60+'秒' txt.text=str
}
倒计时的制作(以系统时间作为基准)
package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main2 extends Sprite
{
private var timer:Timer;
private var num:Number;
private var txt:TextField;
private var txt1:TextField;
private var loader:Loader;
public function Main2()
{
num = 0;
timer = new Timer(1);
//trace(timer.running);
timer.start();
txt = new TextField();
stage.addChild(txt);
txt.x = 100;
txt.y = 50;
txt.width = 100;
txt.height = 30;
txt.border = true;
txt.selectable = false;
txt.text = "倒计时";
loader = new Loader();
loader.load(new URLRequest("001.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderInfoHandler);
txt1 = new TextField();
stage.addChild(txt1);
txt1.x = 100;
txt1.y = 100;
txt1.width = 350;
txt1.height = 30;
txt1.border = true;
txt1.selectable = false;
txt1.text = "倒计时:";
timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteHandler);
}
private function onLoaderInfoHandler(event:Event):void
{
stage.addChild(loader);
loader.addEventListener(MouseEvent.CLICK,onLoaderHandler);
}
private function onLoaderHandler(event:MouseEvent):void
{
txt1.appendText(txt.text + "/")
}
private function onTimerCompleteHandler(event:TimerEvent):void
{
trace("计时完成!");
}
protected function onTimerHandler(event:TimerEvent):void
{
txt.text = String(timer.currentCount);
//num++;
//if (num == 5)
//{
// timer.stop();
//}
//trace(num);
}
}
}