Flash AS3,如何暂停循环秒

时间:2017-05-06 22:51:21

标签: loops actionscript-3 flash

我有这个AS3脚本(它工作正常),我只想让循环暂停几秒钟,然后它可以继续循环。就像我喜欢它停止Milliseconds.Thanks

var myText:String; 
var counter:int = 0; 

var format : TextFormat = new TextFormat();
format.size = 16;
format.font = "Verdana";
format.bold = true;
format.color = 0x000000; 

var textField : TextField = new TextField();
textField.width = 200;
textField.height = 50;
textField.selectable = false;
textField.wordWrap = true;
textField.defaultTextFormat = format;
textField.x = textField.y =0;
addChild(textField);
				
var textLoader:URLLoader = new URLLoader(new URLRequest("text.txt"));
textLoader.addEventListener(Event.COMPLETE, function(e:Event){initText(e.target.data);});

function initText(string:String):void{
	myText = string; 
	addEventListener(Event.ENTER_FRAME, writeText); 
}

function writeText(e:Event):void{
	if (counter <= myText.length){
   	     textField.text = myText.substr(0,counter); 
   	     counter++;
         /*What I can put here to make it pause for a while*/
	}
	else{
		removeEventListener(Event.ENTER_FRAME,writeText); 
	}
}

1 个答案:

答案 0 :(得分:2)

您的代码很好,您需要稍微调整一下。

function initText(string:String):void
{
    myText = string; 
    addEventListener(Event.ENTER_FRAME, writeText); 
}

// Variable to keep the next print time in milliseconds.
var nextPrint:int;

function writeText(e:Event):void
{
    // Function getTimer() returns time in milliseconds since app start.
    // Skip this frame if time is not right.
    if (getTimer() < nextPrint) return;

    // Variable nextPrint is initially 0 so the first char will print immediately.

    if (counter <= myText.length)
    {
        textField.text = myText.substr(0, counter); 
        counter++;

        /*What I can put here to make it pause for a while*/
        // Print next character in ~100 ms.
        nextPrint = getTimer() + 100;
    }
    else
    {
        removeEventListener(Event.ENTER_FRAME, writeText); 
    }
}