actionscript 3 sleep?

天大地大妈咪最大 提交于 2019-12-31 01:50:10

问题


I have a simple actionscript function

var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);

function frameLooper(event:Event):void {
    if(myArray.length > 0) {
        text1.appendText(myArray.shift());
    }else{
        removeEventListener(Event.ENTER_FRAME, frameLooper);
    }
}

And I want to have it sleep after calling the framelooper so it is a little bit slower. How could I do this?

btw, I'm fairly new and found this code on a tutorial, it's a text typing effect, if there is a better way of doing this please let me know.


回答1:


Use a Timer:

var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
var timer : Timer = new Timer (1000, myArray.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();

function frameLooper(event:Event):void {
    text1.appendText(myArray.shift());
}

This will execute the frameLooper on every second for exactly as many times as the length of the array.




回答2:


I'm not saying this is better than the timer method, just an option

var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);

const WAIT_TIME:int = 10;
var i:int = 0;
function frameLooper(event:Event):void {
    if(myArray.length > 0) {
        if(i==0){ 
            trace(myArray.shift()); 
            i = WAIT_TIME;
        };
    } else {
        removeEventListener(Event.ENTER_FRAME, frameLooper);
    }
    i--;
}


来源:https://stackoverflow.com/questions/5656706/actionscript-3-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!