Skip navigation
Currently Being Moderated

flashcards activity problem: matching image and answer arrays

Sep 17, 2012 8:21 AM

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

 
Replies
  • Currently Being Moderated
    Sep 17, 2012 8:42 AM   in reply to athniel

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 5:57 AM   in reply to athniel

    Try this:

     

    I cleaned it up a bit and made it more readable.

     

    Functionally the key is to hold related pictures and answers in the same entity - Array questions in this case.

     

     

    //import classes
    import flash.utils.getDefinitionByName;
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.text.*;
    import flash.events.MouseEvent;
     
    var intro:TextField;
    var theAnswer:TextField;
     
    var goStart:startMe;
    var checkMe:amRight;
    var nextFC:goNext;
     
    var questions:Array = [];
    var nextCard:int = 0;
     
    setUI();
    createQuestions();
     
    function createQuestions():void
    {
        var allAnswers:Array = ["Flower", "Landscape", "Shrub", "Jellyfish"];
        var thePic:Class;
        for (var i:int = 1; i <= allAnswers.length; i++)
        {
            thePic = getDefinitionByName("pic" + i) as Class;
            questions.push({pic: new thePic(), answer: allAnswers[i]});
        }
    }
     
    function placeCard():void
    {
        nextCard = Math.random() * questions.length;
        holder.addChild(questions[nextCard].pic);
    }
     
    //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;
        nextFC.visible = true;
        theAnswer.text = questions[nextCard].answer;
    }
     
    function setUI():void
    {
        var introFormat:TextFormat = new TextFormat("Verdana", 12, 0x000000);
        introFormat.leftMargin = 5;
        introFormat.rightMargin = 5;
        //intro instructions
        var subject:String = "Test Cards";
        var instruct:String = "These flashcards help you practise identifying " + subject + "." + "\r" + "The flashcards will appear in a different order each time you play." + "\r" + "\r" + "* Select 'Start' to reveal the first image." + "\r" + "* Select 'Am I right?' to reveal the answer." + "\r" + "* Select 'Next flashcard' to continue." + "\r" + "* To exit, close [x] this window.";
        intro = new TextField();
        intro.x = 5;
        intro.y = 5;
        intro.width = 290;
        intro.defaultTextFormat = introFormat;
        intro.autoSize = TextFieldAutoSize.LEFT;
        intro.background = true;
        intro.backgroundColor = 0xF4EDDC;
        intro.border = true;
        intro.borderColor = 0x000066;
        intro.selectable = false;
        intro.text = subject + "\r" + "\r" + instruct;
        addChild(intro);
        //set up answer field
        theAnswer = new TextField();
        theAnswer.x = 5;
        theAnswer.y = 5;
        theAnswer.width = 290;
        theAnswer.height = 220;
        theAnswer.defaultTextFormat = introFormat;
        theAnswer.wordWrap = true;
        theAnswer.background = true;
        theAnswer.backgroundColor = 0xF4EDDC;
        theAnswer.border = true;
        theAnswer.borderColor = 0x000066;
        theAnswer.selectable = false;
        addChild(theAnswer);
        theAnswer.visible = false;
        //add image holder
        var holder:picHolder = new picHolder();
        addChild(holder);
        //add buttons, call start function
        goStart = new startMe();
        checkMe = new amRight();
        nextFC = new goNext();
        goStart.x = stage.stageWidth / 2 - goStart.width / 2;
        goStart.y = 230;
        addChild(goStart);
        checkMe.x = stage.stageWidth / 2 - checkMe.width / 2;
        checkMe.y = 230;
        addChild(checkMe);
        checkMe.visible = false;
        nextFC.x = stage.stageWidth / 2 - nextFC.width / 2;
        nextFC.y = 230;
        addChild(nextFC);
        nextFC.visible = false;
        //call answer function
        checkMe.addEventListener(MouseEvent.CLICK, showAnswer);
        //next card button and random image function: toggle answer & button visibles, call random function
        nextFC.addEventListener(MouseEvent.CLICK, swapButtons);
        goStart.addEventListener(MouseEvent.CLICK, startCards);
    }
     
    //start function: remove intro & start button, show first (random) card, shoe check button;
    function startCards(event:MouseEvent):void
    {
        removeChild(intro);
        removeChild(goStart);
        placeCard();
        checkMe.visible = true;
    }
     
    function swapButtons(event:MouseEvent):void
    {
        theAnswer.visible = false;
        nextFC.visible = false;
        checkMe.visible = true;
        placeCard();
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 23, 2012 6:59 AM   in reply to athniel

    Oh , I just noticed that your pic<int> classes start with 1 - not 0. allAnswers array doesn't match that

     

    In this case, try:

     

     

    function createQuestions():void
    {
        var allAnswers:Array = ["blank", "Flower", "Landscape", "Shrub", "Jellyfish"];
        var thePic:Class;
        for (var i:int = 1; i <= allAnswers.length; i++)
        {
            thePic = getDefinitionByName("pic" + i) as Class;
            questions.push({pic: new thePic(), answer: allAnswers[i]});
        }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 23, 2012 7:19 AM   in reply to 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[i]});
        }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 23, 2012 2:08 PM   in reply to athniel

    You are welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points