2017-05-06 69 views
1

我有這個AS3腳本(它工作正常),我只是想讓循環暫停每秒秒,然後它可以繼續循環。就像如果我喜歡它停下來Milliseconds.ThanksFlash AS3,如何暫停一個循環秒

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); 
 
\t \t \t \t 
 
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{ 
 
\t myText = string; 
 
\t addEventListener(Event.ENTER_FRAME, writeText); 
 
} 
 

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

+0

你可以看看[計時器](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html#includeExamplesSummary)類。嘗試使用它來代替ENTER_FRAME來控制文本更新之間的延遲 –

+0

將該函數剪切爲兩個,併爲'root'的'ENTER_FRAME'添加一個偵聽器,以稍後運行第二部分。 – iBug

回答

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