Skip to main content
Participating Frequently
March 6, 2013
Answered

sequencing of buttons

  • March 6, 2013
  • 1 reply
  • 1914 views

Hi,

I am very new to actionscript programming. I am stuck at finding out how the sequencing of buttons is done?

For Example, i have got 8 buttons on the page which are to be selected in a pre defined order. Upon each selection and if its in the correct sequence, it takes the page to the required scene. Otherwise it should show an error and redirect to the main page.

could someone please help me with this?

This topic has been closed for replies.
Correct answer Ned Murphy

Hi Ned,

I need a help!

I have tried with the coding. I created three scenes:

1st scene: 5 buttons are placed;btn1,btn2,btn3,btn4,btn5 and the order is same as the numbering of buttons

2nd scene: correct page

3rd scene: error page

Now when i click on btn1, it takes me to the correct page. And, if i click on any other button nothing happens.

Below is the code:

var myArray:Array = [btn1,btn2,btn3,btn4,btn5];

var counter:int = 0;

for(var i:int=1;i<=5;i++)

{

this["btn"+i].addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);

function fl_ClickToGoToScene(event:MouseEvent):void

{

          if(myArray[counter] == event.currentTarget)

          {

            counter = counter+1;

            MovieClip(this.root).gotoAndPlay(1, "Scene 3");

            //increment the counter

      //go to the correct scene

          }

          else if(myArray[counter] != event.currentTarget)

          {

     MovieClip(this.root).gotoAndPlay(1, "Scene 4");

           // show an error

          }

 

}

}


The first thing you should do is get rid of using scenes.  They are only a problem waiting to happen.  Either use frames of just the one timeline or use movieclips and control which is visible.  You will find it alot easier to manage of you take the latter approach.

The next thing you need to do is get that function out of the loop you have it in.  You only need to assign multiple listeners... the function is coded generically and only needs to be defined once.

Another thing to avoid doing is using unnecessary root references.  If the code is in the main timeline, there is no need to target it using root.

For your conditional there is no need for the != test, just the else portion is needed.  The first if is enough to determine whether it is true or false... if it ain't true it has to be false....

if(test==true){

} else {

}

1 reply

Ned Murphy
Legend
March 6, 2013

You need to think thru a logic scheme wherein you keep track of the button usage order and compare it to a predefined order based on keeping a count of where you are in that order.

One approach would be to store the instance references of the buttons in an array in the order they need to be for a correct sequence . Then have a counter that starts at 0 and gets incremented each time a button gets used.

When you click a button you check to see if the button that was clicked (the event's currentTarget property will tell you that) matches the button in the array using the counter index...

if(event.currentTarget == orderedButtonsArray[counter]){

      //increment the counter

      // if the end of the array is reached, you sequence was followed correctly

} else {

     // show an error

}

Participating Frequently
March 6, 2013

Hi Ned,

Thank you for the reply.

But i am fairly new to all this. I would really appreciate if could elaborate this with an example? It will help me to understand more clearly.

Ned Murphy
Legend
March 6, 2013

I just gave you an example.  What part of it do you not understand?