前面写了2种文本内容自动滚动的例子,这里写上第三种,用timer事件
1,在mxml中设置:
<mx:Canvas id="ct" bottom="60" top="30" width="100%" x="10" backgroundAlpha="0" verticalScrollPolicy="off" horizontalScrollPolicy="off" >
<mx:Text x="0" width="100%" height="100%" id="scrollTxt" y="-20" themeColor="#FFFFFF" />
</mx:Canvas>
2,在Script中:
import flash.utils.Timer;
import flash.events.TimerEvent;
private var delay:uint = 10; //这里设置时间间期,
private var myTimer:Timer = new Timer(delay); //循环timer,次数是无限
private function init():void
{
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(evt:TimerEvent):void{
if (scrollTxt.htmlText.length == 0)
return;
scrollTxt.y--;
if (-scrollTxt.y == scrollTxt.height)
scrollTxt.y = scrollTxt.height - 50;
}
以上就完成了文本内容滚动的情况,这里采用的方法是当文本的坐标和高度,当文本的坐标(不断移动)为文本的高度时,就重新设置文本的y坐标为底部坐标,从而实现了循环滚动效果