2013-05-14 90 views
2

隨意將代碼複製並粘貼到您的FLASH中。它應該能夠追蹤變量。AS3從字母數組中獲取隨機唯一字符

我正在嘗試創建一個兒童匹配遊戲。它會爲字母表選擇一個字母,並要求他們從3個選項中找到該字母。我也會隨機化他們選擇的3個字母,但它還沒有在這個代碼中。

我的問題是大部分時間是用「POP」刪除陣列變種,但有時我也得到重複,有時它出來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

一個例子洗牌機,從字母表集合拼接字母:

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

+0

我仍然在上面的代碼中得到重複。你知道這可能是爲什麼嗎?我認爲這行會做到這一點Alphabet.splice(idx,1) - 但它沒有 – 2013-05-14 01:08:30

+0

拼接'Alphabet.splice(idx,1)[0]'後採取第一個元素,並確保集合不是重置爲完整的字母表。 – 2013-05-14 01:10:51

+0

我試過這一行,但現在我又得到了空值。 – 2013-05-14 01:13:09

0

方法Array.splice()

和不array.pop();

我想通了。

+0

我根據給出的答案重新編寫了我的代碼。它似乎工作得很好。 – 2013-05-14 01:38:50

相關問題