A Flash Developer Resource Site

Results 1 to 3 of 3

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

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

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

    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.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    There are many many ways you could go about this depending what feels right to you and other technical needs. But lets take a very simple albeit not rock solid approach.
    Typically this kind of thing is done with arrays (which are just lists of things) where each button press would add an item to the list and then the whole list would be checked one item at a time to see if it's in the right order.

    You can however get similar control by comparing strings. Basically you'll have your correct answer string, say "1234" and you'll have the students string which will be what ever order they clicked in so something like "1423". So each on each button click you'll compare the two to see if they match (answer == student ?). The thing to keep in mind is that upon the first click you'll only have one number, two on the second click and so on. So in addition to checking if the values match, you'll to need to check if the number of numbers match. Since they're strings you're comparing the number of characters (they're not actually numbers at this point). This is known as a String's length.

    So if the lengths are the same and they match, you can move to the next frame, if the lengths are the same and the don't match, you'll have to respond differently.

    Here's what it might look like (I'm assuming you're using AS3):
    Code:
    //here we define the two value strings
    var answer:String = "1234";
    var student:String = ""; // we give it an empty string as a default value because without a value at all you'd get an error saying it was null
    
    // this is the function each button's event handler will call
    // we're going to assign each button their own unique "id" which we'll check here to find out who was clicked
    function onWordClick(event:Event):void{
      var id:String = event.target.id;
      student += id;
      if(student.length == answer.length){
        // check the values, otherwise skip this part
        if(student == answer){
          // correct!
        }else{
          // incorrect
        }
      }
    }
    
    // now assign each button their unique id and a click handler
    btn1.id = "1";
    btn1.addEventListener(MouseEvent.CLICK, onWordClick);
    
    btn2.id = "2";
    btn2.addEventListener(MouseEvent.CLICK, onWordClick);
    
    btn3.id = "3";
    btn3.addEventListener(MouseEvent.CLICK, onWordClick);
    
    btn4.id = "4";
    btn4.addEventListener(MouseEvent.CLICK, onWordClick);

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Hey,
    First, thanks for helping.

    It keeps giving me an error on the button id. lines:
    Scene 1, Layer 'Buttons', Frame 1, Line 19 1119: Access of possibly undefined property id through a reference with static type flash.display:SimpleButton.

    what am I doing wrong?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center