Skip to main content
Inspiring
September 17, 2012
Answered

flashcards activity problem: matching image and answer arrays

  • September 17, 2012
  • 1 reply
  • 2129 views

Hi there, I hope someone can help...

I'm building a Flashcard activity:

     click button -> show random image (from dynamic array);

     click button -> show answer (from another array) that matches image.

I have the random images part sorted; but having problems identifying the current image shown so that its index can be matched against the answer array's index.

Here are relevant code bits:

//add image holder

var holder:picHolder = new picHolder();

addChild(holder);

//set up image array and random variable

var allPics:Array = [];

for (var i:int = 1; i<=numPics; i++)

{

var thePic:Class = getDefinitionByName("pic" + i) as Class;

var myPic:MovieClip = new thePic();

myPic.name = "pic" + i;

allPics.push(myPic);

}

var nextCard = Math.round(Math.random() * allPics.length - 1);

//next card button and random image function: toggle answer & button visibles, call random function

nextFC.addEventListener(MouseEvent.CLICK, swapButtons);

function swapButtons(event:MouseEvent):void

{

theAnswer.visible = false;

nextFC.visible = false;

checkMe.visible = true;

placeCard(event);

}

function placeCard(event:MouseEvent):void

{

nextCard = Math.round(Math.random() * allPics.length - 1);

nextCard = (nextCard > -1) ? nextCard : 0;

holder.addChild(allPics[nextCard]);

}

var allAnswers:Array = ["Flower","Landscape","Shrub","Jellyfish"];

//call answer function

checkMe.addEventListener(MouseEvent.CLICK, showAnswer);

//answer function: hide check button, show answer and next button;

function showAnswer(event:MouseEvent):void

{

checkMe.visible = false;

holder.removeChild(allPics[nextCard]);

theAnswer.visible = true;

//test

theAnswer.text = "description here - need match answer in allAnswers array with random image from allPics array.";

//end test

nextFC.visible = true;

}

Many thanks for any help anyone can give to solve this .

ps:

If you like I can paste in whole code in case it makes it easier to see what I'm trying to do (acn't see a way to upload FLA to this forum).

This topic has been closed for replies.
Correct answer athniel

Second though. The following is better - more natural. Note getDefinitionByName("pic" + (i + 1))

function createQuestions():void

{

    var allAnswers:Array = ["Flower", "Landscape", "Shrub", "Jellyfish"];

    var thePic:Class;

    for (var i:int = 0; i < allAnswers.length; i++)

    {

        thePic = getDefinitionByName("pic" + (i + 1)) as Class;

        questions.push({pic: new thePic(), answer: allAnswers});

    }

}


Hi Andrei

The previous version gave error, but this one seems to work perfectly.

Many thanks !

1 reply

Ned Murphy
Legend
September 17, 2012

You should be able to assign the index to a variable just like you do the name so that you can use that to check against the answers index values.

athnielAuthor
Inspiring
September 18, 2012

Hi Ned

Have tried various ways to do that, but nothing works. The problem (for me) is the randomness; it's not something as simple as indexOf() or similar - at least not in a way I can figure out (have been away from Flash for months as it's only part of what  do, so I get rusty).

If anyone can offer a solution to matching the ordered answer indices with the random pic indices  -  or whatever else works - that would be great. Thanks