Steven
Steven

Reputation: 13995

actionscript 3 sleep?

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.

Upvotes: 3

Views: 4717

Answers (2)

Chris
Chris

Reputation: 58292

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--;
}

Upvotes: 0

weltraumpirat
weltraumpirat

Reputation: 22604

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.

Upvotes: 7

Related Questions