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).
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 ![]()
The best I can do is show the [B]last answer only [/B]- no matter what image is showing. So I think maybe I'm not too far way, but I've evidently done something wrong!
Here is the full the script which sits in the first frame.
[as]
//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;
//set number of images and intro text and answers array
var numPics:int = 4;
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.";
var allAnswers:Array = ["Flower","Landscape","Shrub","Jellyfish"];
//intro instructions
var introFormat:TextFormat = new TextFormat("Verdana",12,0x000000);
introFormat.leftMargin = 5;
introFormat.rightMargin = 5;
var intro:TextField = new TextField();
intro.x = 5;
intro.y = 5;
intro.width = 290;
intro.defaultTextFormat = introFormat;
intro.autoSize = TextFieldAutoSize.LEFT;
intro.wordWrap = true;
intro.background = true;
intro.backgroundColor = 0xF4EDDC;
intro.border = true;
intro.borderColor = 0x000066;
intro.selectable = false;
intro.text = subject + "\r" + "\r" + instruct;
addChild(intro);
//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:int;
//set up answer field
var theAnswer:TextField = 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 buttons, call start function
var goStart:startMe = new startMe();
var checkMe:amRight = new amRight();
var nextFC:goNext = 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;
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(event);
checkMe.visible = true;
}
//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;
nextFC.visible = true;
for (var j:int = 0; j < numPics; j++)
{
myPic.answer = allAnswers[j];
}
theAnswer.text = myPic.answer;
}
//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]);
}
[/as]
The critical bit is matching the the correct answer from allAnswers with random image from allPics .
If anyone can help me with the correct solution that would be great :).
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();
}
Hi Andrei
Thanks. We're almost there, I think. I had to sort the holder by inserting:
var holder:picHolder = new picHolder();
addChild(holder);
and changing:
holder.removeChild(allPics[nextCard]);
to:
holder.removeChild(questions[nextCard].pic);
But still have a probem with the answers. The answers are coming through but they still do not match the images - they seem to be coming through at random. Also after a few images I get this error:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
I'm sure we're almost there. Thanks for your patience. If you can help in finishing this off that would be great ![]()
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]});
}
}
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]});
}
}
North America
Europe, Middle East and Africa
Asia Pacific