2

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

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

我的问题是大多数时候它是使用“ POP ”删除数组变量,但有时我会得到DUPLICATES,有时它会出现NULL。我在这里做错了什么?

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

2 回答 2

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, B

你能找到字母: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, E

你能找到字母: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

于 2013-05-14T00:53:32.810 回答
0

array.splice()

而不是 array.pop();

我想到了。

于 2013-05-14T00:46:26.290 回答