AS3从Alphabet数组中获取随机唯一字符

时间:2013-05-14 00:42:43

标签: arrays actionscript-3

随意将代码复制并粘贴到fla中。它应该可以跟踪变量。

我正在尝试创建一个匹配游戏的孩子。它为字母表选择一个字母,并要求他们从3个选项中找到该字母。我也将随机选择他们选择的3个字母,但它还没有在此代码中。

我的问题是大多数情况下它是使用“ POP ”删除数组var但有时我得到 DUPLICATES ,有时会出现 NULL < / strong>即可。我在这做错了什么?

import flash.events.MouseEvent;
import flash.display.*;

/// Array of the Alphabet
var Alphabet:Array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

// Arry to hold 3 unique letters
var randArray:Array = new Array();

function getRandomElementOf(array:Array):Object 
{
    var idx:int=Math.floor(Math.random() * array.length);
    // Supposed to remove the letter so can't be chosen again
    array.pop()

    // Adds 1 of 3 letters to new array
    randArray.push(array[idx]);
    return array[idx];
}


function testArray(evt:MouseEvent){


var One = getRandomElementOf(Alphabet);
trace(One);
var Two = getRandomElementOf(Alphabet);
trace(Two);
var Three = getRandomElementOf(Alphabet);
trace(Three);

trace("Can you find the letter " + One + "?   " + randArray);

// Resets the random Array
randArray = new Array();
// Resets the letters forto be chosen again.
Alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
}

/// button to click stage to test vars
stage.addEventListener(MouseEvent.CLICK, testArray);

2 个答案:

答案 0 :(得分:4)

示例洗牌器,拼接字母集合中的字母:

var alphabet:Vector.<String> = new <String>[ "A", "B", "C", "D", "E", "F", "G",
                                             "H", "I", "J", "K", "L", "M", "N",
                                             "O", "P", "Q", "R", "S", "T", "U",
                                             "V", "W", "X", "Y", "Z" ];

while (alphabet.length > 0)
{
    var letter:String = alphabet.splice(int(Math.random() *
                                            alphabet.length), 1)[0];
    trace(letter);
}

示例输出:

  

V,M,F,E,D,U,S,L,X,K,Q,H,A,I,W,N,P,Y,J,C,T,O,R,G ,B,Z

应用于您的示例,这里有一个重置函数,用于将字母表集合重置回原始状态,一个随机字母函数,用于从字母表集合中删除单个字母,以及一个随机化字母集合的随机函数:

/** Alphabet collection */
var alphabet:Vector.<String>;

/** Reset alphabet */
function reset():void
{
    alphabet = new <String>[ "A", "B", "C", "D", "E", "F", "G",
                             "H", "I", "J", "K", "L", "M", "N",
                             "O", "P", "Q", "R", "S", "T", "U",
                             "V", "W", "X", "Y", "Z" ];
}

/** Get random letter from alphabet */
function getRandomLetter():String
{
    return (alphabet.splice(int(Math.random() *
                                alphabet.length), 1)[0]);
}

/** Shuffle alphabet collection */
function shuffleAlphabet():Vector.<String>
{
    var alphabetShuffled:Vector.<String> = new Vector.<String>();

    while (alphabet.length > 0)
    {
        alphabetShuffled.push(getRandomLetter());
    }

    return alphabetShuffled;
}

以下内容从字母表中拉出一个随机字母,并显示整个字母组合:

// get a random letter:
reset();
var randomLetter:String = getRandomLetter();

trace("Can you find the letter: " + randomLetter + "?");

// display entire alpha shuffled:
reset();
trace(shuffleAlphabet());

游戏输出示例:

  你能找到这封信:Q?
  R,I,U,J,Y,d,K,W,T,F,N,G,A,P,X,H,Q,L,S,O,C,V,M,Z,E,乙
  
  你能找到这封信:P?
  I,F,C,S,J,P,Q,M,d,T,H,X,O,V,W,G,K,A,N,Y,L,U,Z,R,B, Ë
  
  你能找到这封信:S?
  B,U,O,S,C,N,I,E,W,L,P,Q,Z,R,A,G,J,K,Y,M,T,V,X,d,H, ˚F

答案 1 :(得分:0)

方法Array.splice()

而不是array.pop();

我明白了。