Flex Timer小例子

本文介绍了一个使用 Flex 的 Timer 类实现的跑马灯示例,该示例能够动态调整时间间隔,展示了 Timer 相对于 setInterval 的优势。通过调整代码,可以创建更多有趣的动态效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值