Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now