Skip to main content
Participating Frequently
December 22, 2010
Answered

using buttons to select the correct answer

  • December 22, 2010
  • 1 reply
  • 584 views

I have a simple game I am attempting to write. A random movie clip (from the library) is displayed on the stage from an array of mc's.  Here's the question.  What my game will do, is allow the user to select one of two buttons (red and green) based on the movieclip that is displayed.  One will yield a correct result and the other will yield an incorrect result.  HOWEVER, depending on the movie clip displayed, sometimes the red button will yield the correct result and sometimes the green button will yield the correct result.  I am having trouble figuring out the best logic in writing code to accomplish this.  Thanks in advance for your advice.

This topic has been closed for replies.
Correct answer dtauer

There are a lot of different ways to set this up, but I'll try to suggest one that will be easiest to implement given your set up.  Create one more Array that will keep track of which button is correct. This Array will be the same size as the MovieClip Array.  It will have the instance name of the correct button in each position.  Here's some sample code:

//I'm assuming the red and green buttons on your stage have instance names of red_btn and green_btn respectively.

var clips:Array = [mc1, mc2, mc3, mc4];  //this is your array of MovieClips that you already have

//This array keeps track of which button is the correct button for each clip

//The first entry goes with the first entry of the clips array

var correctButtons:Array = [red_btn, red_btn, green_btn, red_btn]; 

//Lastly, you need a variable to keep track of which MovieClip you placed on the stage

var current:Number;

//You have your logic here to put the MC on the stage.  Also, update the current variable so you know which MC you added

//I'll just pretend you added the first one (0);

current = 0;

//Events for you buttons:

red_btn.addEventListener(MouseEvent.CLICK, checkButton);

green_btn.addEventListener(MouseEvent.CLICK, checkButton);

function checkButton(e:MouseEvent):void

{

     //use the event object to figure out which button was clicked (since they both call the same function

     var clickedButton:SimpleButton = e.currentTarget as SimpleButton;

     //Grab who the correct button should be from our array

     var correctButton:SimpleButton = correctButtons[current];

     //Check to see if they are the same

     if(clickedButton == currentButton)

     {

          trace("correct!!!")

     }

     else

     {

          trace("wrong!");

     }

}

1 reply

dtauerCorrect answer
Participating Frequently
December 23, 2010

There are a lot of different ways to set this up, but I'll try to suggest one that will be easiest to implement given your set up.  Create one more Array that will keep track of which button is correct. This Array will be the same size as the MovieClip Array.  It will have the instance name of the correct button in each position.  Here's some sample code:

//I'm assuming the red and green buttons on your stage have instance names of red_btn and green_btn respectively.

var clips:Array = [mc1, mc2, mc3, mc4];  //this is your array of MovieClips that you already have

//This array keeps track of which button is the correct button for each clip

//The first entry goes with the first entry of the clips array

var correctButtons:Array = [red_btn, red_btn, green_btn, red_btn]; 

//Lastly, you need a variable to keep track of which MovieClip you placed on the stage

var current:Number;

//You have your logic here to put the MC on the stage.  Also, update the current variable so you know which MC you added

//I'll just pretend you added the first one (0);

current = 0;

//Events for you buttons:

red_btn.addEventListener(MouseEvent.CLICK, checkButton);

green_btn.addEventListener(MouseEvent.CLICK, checkButton);

function checkButton(e:MouseEvent):void

{

     //use the event object to figure out which button was clicked (since they both call the same function

     var clickedButton:SimpleButton = e.currentTarget as SimpleButton;

     //Grab who the correct button should be from our array

     var correctButton:SimpleButton = correctButtons[current];

     //Check to see if they are the same

     if(clickedButton == currentButton)

     {

          trace("correct!!!")

     }

     else

     {

          trace("wrong!");

     }

}

jammoseAuthor
Participating Frequently
December 23, 2010

Thanks dtauer.  That looks like a great approach!