I have a working array and a loop with this array. I just have a trace action in my loop so I know that it is looping. My question is how would I be able to display the next image array when the right/wrong answer is selected. My code is below and any help would be appreciated :)
var characterArray:Array = new Array();
characterArray[0] = [pirateboy, pirategirl, pig]; //pig is the ans
characterArray[1] = [pirategirl, pirategirl ,pirateboy]; //pirateboy is the ans
characterArray[2] = [pirategirl, pirateboy, pirateboy]; //pirategirl is the ans
characterArray[3] = [parrot, snowman, parrot]; //snowman is the ans
pig.addEventListener(MouseEvent.CLICK, rightanswer);
function rightanswer (event:MouseEvent){
if (MovieClip(event.target) == characterArray[0][2])
{
gotoAndStop(3)
}
}
pirateboy.addEventListener(MouseEvent.CLICK, rightanswer1);
function rightanswer1 (event:MouseEvent){
if (MovieClip(event.target) == characterArray[1][2])
{
gotoAndStop(3)
}
}
for(var i:int = 0; i<3; i++) {
trace("game");
} //loops array
1 Answer 1
Your code needs some restructuring.
Store the index of the active characterArray subarray.
var characterArray:Array = new Array();
characterArray[0] = [pirateboy, pirategirl, pig]; //pig is the ans
characterArray[1] = [pirategirl, pirategirl ,pirateboy]; //pirateboy is the ans
characterArray[2] = [pirategirl, pirateboy, pirateboy]; //pirategirl is the ans
characterArray[3] = [parrot, snowman, parrot]; //snowman is the ans
pig.addEventListener(MouseEvent.CLICK, onCharacterClick);
pirateboy.addEventListener(MouseEvent.CLICK, onCharacterClick);
pirateGirl.addEventListener(MouseEvent.CLICK, onCharacterClick);
snowman.addEventListener(MouseEvent.CLICK, onCharacterClick);
var currentQuestion:int = 0;
function onCharacterClick(event:MouseEvent):void
{
switch(currentQuestion)
{
case 0:
if(event.target == pig)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 1:
if(event.target == pirateboy)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 2:
if(event.target == pirategirl)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 3:
if(event.target == snowman)
{
currentQuestion++;
gotoAndStop(3);
}
break;
default:
break;
}
}
This stores the current set of characters. You can access that specific array using characterArray[currentQuestion]
. If you wanted to display each character choice, you can loop through the current characterArray like this:
for(var i:int = 0; i < characterArray[currentQuestion].length; i++)
{
//add characterArray[currentQuestion][i] to the displaylist
}