A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Keyboard-controlled menu system

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    37

    resolved [RESOLVED] Keyboard-controlled menu system

    Does anyone know of a good way to create a menu system that is completely controlled by the keyboard? I am currently making an RPG and really want to be able to control the whole game with just the keyboard. I'm using AS3, and OOP.

    Let's say you have two characters which you can control in a fight. Both of them would have a character Icon, which you select with left/right arrowkeys. You pick one using SPACE, and then an attack menu pops up above the icon. now you can select different options like "attack" and "guard" with up/down keys. lets say you pick attack. now you have to pick an enemy to attack, from 3 enemies. an indication symbol pops up above the currently selected enemy, and now you can use left/right again to choose which enemy to attack. Does this make sense to anyone? How could I structure this system, in the simplest manner possible? Should I make each menu symbol extend a class which is manipulated by the keyboard? how would such a class look?

    I'm going for a "Breath of Fire"/"Golden Sun" type combat system, if that is helpful to anyone. Hope someone can help!

  2. #2
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Well I'm not too sure what part is giving you the trouble but basically all you have to do is listen for Keyboard events.

    Go here:

    http://www.flashessential.com/archives/80

    There's simple example of how you listen for keys.

    You would just attach the eventlistener to the stage and then depending what part of your combat your in (picking an opponent, going through a menu, etc...) then you make the key do the appropriate action.

    To structure it, I would probably have a variable that holds what is going on at the moment. Just have a string with the current course of action, ie:

    var currentAction:String = "pickOpponent";

    Then switch between the different possible scenarios when you get a key.

    So something like

    function KeyPressed(evt:KeyboardEvent):void {
    switch (evt.keyCode) {
    case Keyboard.UP :

    switch (currentAction) {

    case "pickOpponent":

    cycleOpponents();

    break;

    case "pickMenu":

    cycleMenu();

    break;
    }

    break;
    }

    }

  3. #3
    Member
    Join Date
    Sep 2010
    Posts
    37
    Aaah ofc, that makes total sense. That's probably exactly what I was looking for, just didn't think of how to go about doing it. Definately going to try that, Beathoven. I'll post a reply later telling how it went.

    Thanks mate!

  4. #4
    Member
    Join Date
    Sep 2010
    Posts
    37
    Okay, the system has worked super until recently, when I noticed that sometimes the keyEvent fires several "actions" in quick succession, like so:
    Code:
    case Keyboard.SPACE :    //confirms your current button-selection
         switch (currentAction) {
              case "pickAttackType":
                    if(attackBtnSelected){
                    cycleOpponents(); //function that changes currentAction to "pickOpponent", among other things
                    }
                    if(itemBtnSelected){
                    cycleItems();
                    }
               break;
               case "pickOpponent":
                    attackEnemy(currentEnemyChoice);
               break;
               default:
               break;
          }
    So now, say you press space when currentAction is "pickAttackType", you call cycleOpponents(), and "pickOpponent" now becomes the currentAction.
    immediately, you now attack the first enemy without being able to make your choice, seemingly because the SPACE-event still is being fired after you pressed it to activate cycleOpponents();
    The same is true if you pick Items: the first item is automatically chosen. But I still don't quite understand why, shouldnt the keyEvent "break" away after it registers the initial keypress and activates one of the cases in the switch conditional?
    How can I counter this?
    Last edited by Jumpkut; 09-30-2010 at 10:51 AM.

  5. #5
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Just pull the cycleOpponent() out of the switch statement and put it at the end.

  6. #6
    Member
    Join Date
    Sep 2010
    Posts
    37
    ...It's always just that simple, isn't it
    haha.
    thanks!

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