Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Button Sequencing (how to code so that buttons must be pushed in a specific order)

New Here ,
Feb 27, 2013 Feb 27, 2013

Hi,

I am fairly new to actionscript programming, I've made a few simple games for the students in my class (They are learning English). I am working on a grammar/syntax game right now.


I am trying to figure out how to write code for button sequencing.


Basically, what I want to happen is. I have 4 buttons: btn1, btn2, btn3, btn4.


They need to be pushed in a specific order (ie. Forming a proper sentence). If they are pushed in the specific order, it goes to to the next frame, to make another sentence, and on, and on like that. If they do not push the buttons in the correct order, then the button values (or whatever) reset until the proper sequence is put in. I've been reading about strings and arrays with button use. But still havent figured it out. Could someone please help?


For example, let's say the sequence was simply btn1, btn2, btn3, btn4.

TOPICS
ActionScript
645
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2013 Feb 27, 2013

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

var correctSequenceIndex:int=0;

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

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

//if these are not onstage, assign names: this["btn"+i].name="btn"+i;

}

function buttonF(e:MouseEvent):void{

if(correctSequenceA[correctSequenceIndex]==e.currentTarget{

correctSequenceIndex++;

if(correctSequenceIndex==correctSequenceA.length){

gotoAndStop(whereever);

}

} else {

correctSequenceIndex=0;

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2013 Mar 03, 2013

Hey,

first, thanks a lot for helping. I am a little confused at the "assigning a name if they aren't on stage"

Basically, your code is all I have in there, for now. I chaged it like this, but I am assuming it is wrong, as it shows me the first frame, but doesn't go to the next frame after pushing the buttons:

----------------

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

var correctSequenceIndex:int=0;

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

this["btn1"+i].addEventListener(MouseEvent.CLICK,buttonF);

this["btn2"+i].addEventListener(MouseEvent.CLICK,buttonF);

this["btn3"+i].addEventListener(MouseEvent.CLICK,buttonF);

this["btn4"+i].addEventListener(MouseEvent.CLICK,buttonF);

//if these are not onstage, assign names: this["btn"+i].name="btn"+i;

}

function buttonF(e:MouseEvent):void{

if(correctSequenceA[correctSequenceIndex]==e.currentTarget){

correctSequenceIndex++;

if(correctSequenceIndex==correctSequenceA.length){

gotoAndStop(2);

}

} else {

correctSequenceIndex=0;

}

}

--------------

Just to keep it simple for now, I only have the one layer with the button in it (I know enough that the buttons are "buttons", and are named btn1, btn2, so forth in the properties.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2013 Mar 04, 2013
LATEST

the first error i see is your for-loop: you didn't copy the code i suggested.  use:

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

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

//if these are not onstage, assign names: this["btn"+i].name="btn"+i;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines