Copy link to clipboard
Copied
I have a math game I'm working on and I would like to have the answers appear in random positions. For example:
3 x 11 = ?
9 0 33
The answer is obviously 33, position 3. Right now the answer will appear in position 3 every time.
In the next problem I don't want the answer to appear in the same position. I want it to appear in another position randomly. So for example the next problem will have the answer in position 1:
3 x 4 = ?
12 14 73
I am currently putting the answers in a dynamic text box that is already on the stage. I thought about using an array but I can't figure that part out.
Example screen shot:
Copy link to clipboard
Copied
if you have 3 textfields (eg, tf1,tf2,tf3) for the answers, you could use:
var answersA:Array=[[x11,x12,x13],...,[9,0,33],...,[xn1,xn2,xn3]];
where the 3 answer choices for question 1 is x11,x12,x13 and the 3 answer choices for the nth question is xn1,xn2,xn3
you could use:
function displayAnswersF(i:int):void{
// display answers for the ith question
var answerA:Array = answersA;
answerA.shuffle();
tf1.text=answerA[0].toString();
tf2.text=answerA[1].toString();
tf3.text=answerA[2].toString();
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Copy link to clipboard
Copied
Thanks. I will try today.
Copy link to clipboard
Copied
What is the syntax below? I've seen the wildcard "*" used in import statements (import flash.events.*) to bring in all of a class but I've never seen it used with a data type.
Is this saying the "t" can by of any kind of data type?
var t:*;
Copy link to clipboard
Copied
yes.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now