Feel free to copy and paste the code into your fla. It should work to trace vars.
I am trying to create a kids matching game. It selects one letter for the alphabet and will ask them to find that letter from 3 choices. I am also going to randomize the 3 letters they pick from but it is not yet in this code.
My issue is most of the time it is removing an array var using "POP" but sometimes and I get DUPLICATES and sometimes it comes out NULL. What am I doing wrong here?
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);