Copy link to clipboard
Copied
`
I'm not overly sure if this is possible, as I am not a frequent programmer, but I have a question.
I've got an array that generates one random word in a text box and then a second array that generates another random word in a different text box. What I want is for when a certain word out of array number one is generated, a certain image appears with it. Here's the code:
var FirstChoice:Array = ["Do this", "Do that"];
var SecondOption:Array = ["while doing this", "while doing that"];
generate_btn.addEventListener(MouseEvent.CLICK, getTask);
function getTask(event:MouseEvent):void {
var randomChoice:Number = Math.floor(Math.random() * firstChoice.length);
var randomTask:Number = Math.floor(Math.random() * secondOption.length);
First_Thing.text = exerciseChoice[randomChoice];
Second_Thing.text = homeTask[randomTask];
}
So for instance, when I click the button and the first array generates "Do this," I want a specific graphic to appear with it. Likewise, if "Do that" generates, I want a different graphic to appear specifically associated with that variable.
Hopefully this is possible 😕 I'm stumped! I asked for help on StackOverflow; however, their advice didn't work. Granted, I only got one response, so I hope people here can help me out!
var FirstChoice:Array = ["Do this", "Do that"];
var SecondOption:Array = ["while doing this", "while doing that"];
var imageA:Array = [mc1,mc2];
var currentImage:MovieClip
function getTask(event:MouseEvent):void {
var randomChoice:Number = Math.floor(Math.random() * firstChoice.length);
var randomTask:Number = Math.floor(Math.random() * secondOption.length);
First_Thing.text = exerciseChoice[randomChoice];
Second_Thing.text = homeTask[randomTask];
if(currentImage){
currentImage.visible=false;
}
currentImage
...Copy link to clipboard
Copied
use an if-else of switch statement to determine which array option was selected and display the correct graphic. you can assign classes to your graphics and generate instances using getDefinitionByName and add them to the display using addChild().
or you can convert all the graphics to movieclips, add them to the display in the ide, assign instance names and use their visible property to display/hide them.
Copy link to clipboard
Copied
Thanks! Any chance you could help me with an example line of that code? Again, I don't have too much programming knowledge, so I'm sure I'll have difficulties figuring out exactly how to type it up. 😕
Copy link to clipboard
Copied
var FirstChoice:Array = ["Do this", "Do that"];
var SecondOption:Array = ["while doing this", "while doing that"];
var imageA:Array = [mc1,mc2];
var currentImage:MovieClip
function getTask(event:MouseEvent):void {
var randomChoice:Number = Math.floor(Math.random() * firstChoice.length);
var randomTask:Number = Math.floor(Math.random() * secondOption.length);
First_Thing.text = exerciseChoice[randomChoice];
Second_Thing.text = homeTask[randomTask];
if(currentImage){
currentImage.visible=false;
}
currentImage=imageA[randomChoice];
currentImage.visible=true;
}
Copy link to clipboard
Copied
Oh man, you're a life saver. Thank you!
The only strange thing I'm trying to figure out is that it's coming back that "randomChoice" is an unidentified property. I think I can figure it out from here, unless you know what the deal is with that.
Copy link to clipboard
Copied
randomChoice (and randomTask) are both local to getTask. if you try and use them outside getTask, they won't be defined.
they are used within getTask in the code i suggested so if you cop and paste that code, there should be no problem. if you need to use them outside getTask, use:
var FirstChoice:Array = ["Do this", "Do that"];
var SecondOption:Array = ["while doing this", "while doing that"];
var imageA:Array = [mc1,mc2];
var currentImage:MovieClip;
var randomChoice:Number;
var randomTask:Number;
function getTask(event:MouseEvent):void {
randomChoice = Math.floor(Math.random() * firstChoice.length);
randomTask = Math.floor(Math.random() * secondOption.length);
First_Thing.text = exerciseChoice[randomChoice];
Second_Thing.text = homeTask[randomTask];
if(currentImage){
currentImage.visible=false;
}
currentImage=imageA[randomChoice];
currentImage.visible=true;
}