Problem matching arrays for flashcards activity: answers to random images
Hi folks
Making a flashcards actvity - self-test, in which user just has to think of the answer and then see if they were right.
How it works:
A button randomly shows an image from a dynamically-produced array of images (movieclips) then switches to a button to show the answer from a static array of strings.
I'm having problem matching the answers to the randomly displayed images. The best I can do is show the last answer only - no matter what image is showing. So I think maybe I'm not too far way, but I've evidently done something wrong!
I've attached the script (rather than the FLA as folk use different versions) which sits in the first frame. Paste this into the first frame to see it working so far.
If anyone can help me with the correct solution that would be great .
Scene 1, Layer 'Layer 1', Frame 1, Line 68 1046: Type was not found or was not a compile-time constant: goNext.
Scene 1, Layer 'Layer 1', Frame 1, Line 35 1180: Call to a possibly undefined method picHolder.
Scene 1, Layer 'Layer 1', Frame 1, Line 66 1180: Call to a possibly undefined method startMe.
Scene 1, Layer 'Layer 1', Frame 1, Line 67 1180: Call to a possibly undefined method amRight.
Scene 1, Layer 'Layer 1', Frame 1, Line 68 1180: Call to a possibly undefined method goNext.
Scene 1, Layer 'Layer 1', Frame 1, Line 67 1046: Type was not found or was not a compile-time constant: amRight.
Scene 1, Layer 'Layer 1', Frame 1, Line 66 1046: Type was not found or was not a compile-time constant: startMe.
Scene 1, Layer 'Layer 1', Frame 1, Line 35 1046: Type was not found or was not a compile-time constant: picHolder.
Hy Rynoe
Sorry, brain must have been frazzled - should really have sent the FLA. Have attached FLA - it's CS4 I'm afraid as that's all I have at home (CS5 at work).
You're very close. You can set up your "answer" variable right away when you're creating the allPics Array:
Code:
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;
myPic.answer = allAnswers[i-1]; // ADDED LINE
allPics.push(myPic);
}
This is a convenient place to set up the answer variable because you're going through the cards one by one anyway.
And then in showAnswer(), when the user has clicked on the "Am I right?" button, get the answer variable from the current card. However, do not use myPic because that actually refers to the myPic in the last pass through the for loop above. The current card is the allPics[nextCard] that you're removing from the card holder.
Code:
function showAnswer(event:MouseEvent):void
{
checkMe.visible = false;
theAnswer.text = allPics[nextCard].answer; // ADDED LINE
holder.removeChild(allPics[nextCard]);
theAnswer.visible = true;
nextFC.visible = true;
}