从actionscript中随机化一个数组

时间:2015-11-16 09:59:26

标签: actionscript-3 random

public class Hangman extends Sprite {
    private var textDisplay:TextField;

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

"我想在这里做的是随机化"短语:String",这样短语结果将被回收,盖章或者爷爷。

    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 个答案:

答案 0 :(得分:1)

您不能使用相同的名称实例化相同的变量...如果需要,请使用数组来保留可能的单词...

allow_redirects

然后,使用Random函数从0中选择一个数字,直到数组大小,然后使用该单词...

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