写了一个timer的跑马灯小例子,发现flex的timer功能还是比setInterval要强,可以动态改变时间间隔,这个功能很有用,可以做出很多有趣的东西来
例子没有写的很仔细,不过改动改动可以做出一些效果来,呵
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var pid:int = 0;
private var tm:Timer;
private function start():void{
sel();
var i:int = this.randRange(56,70);
tm = new Timer(20,i);
tm.addEventListener(TimerEvent.TIMER,onTimer);
tm.addEventListener(TimerEvent.TIMER_COMPLETE,onComplete);
tm.start();
}
private function onTimer(e:TimerEvent):void{
sel();
pid++;
if(pid>13) pid=0;
if(tm.currentCount > 42){
tm.delay = 20 + (tm.currentCount-42)*20;
}
}
private function onComplete(e:TimerEvent):void{
if(pid>0){
Alert.show(main.getChildren()[pid-1].text);
}
else{
Alert.show(main.getChildren()[13].text);
}
tm.removeEventListener(TimerEvent.TIMER,onTimer);
tm.removeEventListener(TimerEvent.TIMER_COMPLETE,onComplete);
tm = null;
}
private function sel():void{
var n:int;
if(pid==0) n = 13;
else n=pid-1;
main.getChildren()[n].setStyle('backgroundColor','white');
main.getChildren()[pid].setStyle('backgroundColor','red');
}
private function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
]]>
</mx:Script>
<mx:Canvas id="main" x="61.5" y="56" width="651" height="455" backgroundColor="#FFFFFF">
<mx:TextArea x="42" y="38" width="83" height="70" text="苹果"/>
<mx:TextArea x="152" y="38" width="83" height="70" text="葡萄"/>
<mx:TextArea x="267" y="38" width="83" height="70" text="柿子"/>
<mx:TextArea x="378" y="38" width="83" height="70" text="李子"/>
<mx:TextArea x="500" y="38" width="83" height="70" text="栗子"/>
<mx:TextArea x="500" y="126" width="83" height="70" text="梨"/>
<mx:TextArea x="500" y="217" width="83" height="70" text="一等"/>
<mx:TextArea x="500" y="315" width="83" height="70" text="二等"/>
<mx:TextArea x="397" y="315" width="83" height="70" text="三等"/>
<mx:TextArea x="267" y="315" width="83" height="70" text="四等"/>
<mx:TextArea x="152" y="315" width="83" height="70" text="五等"/>
<mx:TextArea x="42" y="315" width="83" height="70" text="六等"/>
<mx:TextArea x="42" y="217" width="83" height="70" text="七等"/>
<mx:TextArea x="42" y="126" width="83" height="70" text="八等"/>
</mx:Canvas>
<mx:Button x="352" y="519" label="start" fontSize="12" click="start();"/>
</mx:Application>