2

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

2 Answers 2

4

An example shuffler, splicing letters from the alphabet collection:

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

Example output:

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

Applied to your example, here's a reset function to reset the alphabet collection back to its original state, a random letter function to remove a single letter from the alphabet collection, and a shuffle function to randomize the alphabet collection:

/** 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;
}

The following pulls a random letter from the alphabet to be found and display the entire alphabet shuffled:

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

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

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

Example game output:

Can you find the letter: 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

Can you find the letter: 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

Can you find the letter: 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

Sign up to request clarification or add additional context in comments.

8 Comments

I am still getting duplicates in my above code. Do you know why this might be. I thought this line would do it Alphabet.splice(idx,1) - but it didn't
Take the first element after splicing Alphabet.splice(idx,1)[0] and be sure the collection isn't being reset to the full alphabet.
Example extended to resemble your game play.
Awesome! Thanks so much. That did it. I am not so sure I understand exactly what its doing but I will look it over. Thank you my friend! Excellent!
Vectors are typed collections, which operate faster as Flash Player Runtime does not have to apply type checking. Iteration through the collection is faster, and operations on elements are typed when referenced. Example will work the same replacing Vector.<String> for Array.
|
0

array.splice()

and not array.pop();

I figured it out.

1 Comment

I redid my code based on the answer given. It seems to work quite well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.