sdata 是接受后端传来的数据;
要点:this.parent.removeChild(this)的使用
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="35" height="40" horizontalScrollPolicy="off" verticalScrollPolicy="off"
creationComplete="init()">
<mx:Script>
<![CDATA[
[Bindable]
public var imgUrl:String='';
[Bindable]
public var timer:String = '';
[Bindable]
public var gid:String='';
[Bindable]
public var sdata:Object={};
private var t:int;
private var timer1:Timer;
public function init():void
{
var coloArr:Array = sdata['show'].split('_');
var coloStr:String = '';
//获得最后一位值进行判断 进而对颜色赋值
if(coloArr[coloArr.length-1] == '1'){
coloStr = '#FFFFFF';
}else if(coloArr[coloArr.length-1] == '2'){
coloStr = '#009EFF';
}else{
coloStr = '#FF9E00';
}
txt.setStyle("color", coloStr);//设置文本字体颜色
//在sdata中取出时间
t = sdata['time'];
txt.text = transadvanceTime(t);//对时间进行换算
if(t>0)
{
timer1 = new Timer(1000);
timer1.addEventListener(TimerEvent.TIMER,timerHandler1);
timer1.start()
}
}
private function timerHandler1(event:TimerEvent):void
{
if(t>0)
{
t--
txt.text = transadvanceTime(t);
}
else
{
if(timer1)
{
timer1.removeEventListener(TimerEvent.TIMER,timerHandler1);
timer1.stop();
timer1 = null
}
this.parent.removeChild(this);//重点:时间停止了移除自身
}
}
private function transadvanceTime(timeNum:Number):String
{
var total_seconds:uint = Math.floor(timeNum);
var total_minutes:uint = Math.floor(total_seconds/60);
var total_hours:uint = Math.floor(total_minutes/60);
var total_days:uint = Math.floor(total_hours/24);
var days:uint = total_days;
var hours:uint = total_hours%24;
var minutes:uint = total_minutes%60;
var seconds:uint = total_seconds%60;
return (hours<10?"0"+hours:hours.toString())+":"+(minutes<10?"0"+minutes:minutes.toString());
}
]]>
</mx:Script>
<mx:Image x="5" y="1" width="25" height="25" source="{imgUrl}" visible="{gid==''?false:true}" mouseChildren="false"/>
<mx:Text id="txt" x="1" y="22" width="100%" fontSize="11" color="#ffffff" fontWeight="normal" mouseChildren="false"/>
</mx:Canvas>