2015-11-16 84 views
0
public class Hangman extends Sprite { 
    private var textDisplay:TextField; 

    private var phrase:String = "Recycled" 
    private var phrase:String = "Stamped" 
    private var phrase:String = "grandpa" 

「我想在這裏做的是隨機化‘這句話的數組:字符串’,這樣這句話的結果將被要麼回收,蓋章或爺爺隨機化從ActionScript

private var shown:String; 
    private var numWrong:int; 

    public function Hangman() { 
     // create a copy of text with _ for each letter 
     shown = phrase.replace(/[A-Za-z]/g,"_"); 
     numWrong = 0; 

     ...codes* 
    } 

    public function pressKey(event:KeyboardEvent) { 
     // get letter pressed 
     var charPressed:String = (String.fromCharCode(event.charCode)); 

     // loop through nd find matching letters 
     var foundLetter:Boolean = false; 
     for(var i:int=0;i<phrase.length;i++) { 
      if (phrase.charAt(i).toLowerCase() == charPressed) { 
       // match found, change shown phrase 
       shown = shown.substr(0,i)+phrase.substr(i,1)+shown.substr(i+1); 
       foundLetter = true; 
      } 
     } 

     // update on-screen text 
     textDisplay.text = shown; 

     // update hangman 
     if (!foundLetter) { 
      numWrong++; 
      character.gotoAndStop(numWrong+1); 
     } 
    } 
} 

} 

我希望有人能幫助我在這一個,謝謝。

回答

1

你不能被實例化具有相同名稱相同的變量...如果你想,使用數組來保持可能的話......

var phrase:Array = [ "Recycled", "Stamped", "grandpa", ...]; 

然後,使用一個隨機函數從0選擇一個號碼,最多數組的大小,然後用這個詞......

var word = phrase[Math.floor(Math.random()*phrase.length)]; 
+0

@akmozo固定,感謝擡起頭.. – Bonatti

+0

感謝您的幫助!我做到了,支架應該是方形支架而不是大括號。 :) – Confuser

+0

@Confuser是的,就像akmozo說的,已經修復。祝你好運 – Bonatti