dcsimg
A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: [AS3] Key Combos and Arrays?

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    30

    Exclamation [AS3] Key Combos and Arrays?

    hey, I am creating a game in AS3, what I want to do is when the player presses a key combo for example Left Key+Jump & Kick key, the character performs a move e.g like a kamehameha... I also want to execute the action correctly for example the character doesn't perform a left jump kick as its the same key

    i was thinking of using a array, to handle the key combo...is there any other way of doing this, more efficient or more performance?

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Are you going for a sequential press combo? or a multi-keypress combo? If it's sequential then you will keep track of the keys pressed in order, in an array, and match a set of the last group of entries against a list to determine the combo. If it's the second, the array won't be much help, rather you will have to check all the keys pressed at the moment to determine the effect. Though this will have to be done with the biggest chain first, so A+B+C+D would get priority over A+B+C
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Quote Originally Posted by bluemagica View Post
    If it's the second, the array won't be much help, rather you will have to check all the keys pressed at the moment to determine the effect.
    Not necessarily. You should know now that from your other thread that not all keyboards can support X number of key detections at once. In this case, an array would in fact help. Considering your game's frame rate of course, you could add each keystroke to an array, join that array with a null string and determine if performed a jump kick or not. Something I cooked up quick:

    Code:
    var keys:Array = []; // array to hold key codes
    var timeout:int = 2500; // 2.5 seconds to enter combo
    var jumpkick:String = "7485778075736775";
    
    var comboTimer:Timer = new Timer(timeout, 9999);
    comboTimer.addEventListener(TimerEvent.TIMER, checkCombo);
    comboTimer.start();
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    
    function keyPressed(ke:KeyboardEvent):void {
    	keys.push(ke.keyCode);
    }
    
    function checkCombo(te:TimerEvent):void {
    	trace("Testing combos...");
    	if (keys.join("") == jumpkick) {
    		trace("\tJUMPKICK!");
    		keys = [];
    	} else {
    		keys = [];
    	}
    }
    Just paste it into a blank AS3 fla. Type "jumpkick" after you run the app and it will say "JUMPKICK" in the output if you entered the code correctly. I mention frame rate because you're plugging in a combo across multiple frames so if it takes you 30 frames to enter a combo and your frame rate is 31, it's going to look bad, but it's something to consider!
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  4. #4
    Member
    Join Date
    Jun 2009
    Posts
    30
    thank you both of you!,

    Just paste it into a blank AS3 fla. Type "jumpkick" after you run the app and it will say "JUMPKICK" in the output if you entered the code correctly.
    im not too sure what you mean, also whats the numbers assigned to jumpkick:Sting?

  5. #5
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    @JT1 : what IP did in that jumpkick var, is convert each of the letters of the word "jumpkick" to their individual key codes, so if you press the letters of the word in order quickly, then it will detect it as the combo to display "JUMPKICK".
    But this is the basis of sequential key combos(mortal combat approach), not multi-keypress combos! Also I really don't recommend clearing out the array, I would rather recommend checking a predefined number of entries from the end of the array.
    Anyway if you do follow this approach, keep in mind, in as3, keyDown event will keep firing if you press and hold a key, and sometimes, if you press multiple keys together and release them, all of their keyUPs might not fire, atleast there was that bug in the older player.



    @IP: Could you please extend that code to handle "jump", "kick", "jumpkick"? I might be able to understand it properly that way.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  6. #6
    Senior Member
    Join Date
    May 2009
    Posts
    138
    You want the first keypress to start the timer, not have the timer looping continuously in the background. Otherwise if the timer is at 2.3 when they start entering the combo they have 0.2 seconds to complete it.

  7. #7
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Well, in sequential combo system, you will have an array equal to your largest possible combo chain, and a timer which will clear it after about 1 second. This timer will be reset on any key down event, and also on a key press you need to push the data and then shift the array to maintain it's size.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  8. #8
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Quote Originally Posted by ImprisonedPride View Post
    Something I cooked up quick:
    and people still insist on dissecting the code. The forum is obviously full of starving hyenas.

    Quote Originally Posted by ImprisonedPride
    it's something to consider!
    I nowhere in this post declared that this code was a cure-all fix. I simply presented it as a possibly partial solution the OP could use as inspiration to build something that suits his needs.

    Quote Originally Posted by redjag
    You want the first keypress to start the timer, not have the timer looping continuously in the background. Otherwise if the timer is at 2.3 when they start entering the combo they have 0.2 seconds to complete it.
    It's a tricky situation. You can't check from the end of an array when the array changes instead of using a timer because any single-key actions that happen to be the first key in a large combo will be automatically executed by definition of the system.

    Quote Originally Posted by bluemagica
    @IP: Could you please extend that code to handle "jump", "kick", "jumpkick"? I might be able to understand it properly that way.
    No. It doesn't work that way. In most combo systems you don't "perform" each part of the attack. Granted that in this situation, unless a single key caused the player to jump then kick to perform a "jumpkick" the actions should be separated and treated as such.

    Quote Originally Posted by bluemagica
    But this is the basis of sequential key combos(mortal combat approach), not multi-keypress combos!
    And just how would you handle a multi-key press combination that used the same key multiple times? As I already said in most cases the player's keyboard is limited to 3 or 4 simultaneously detected key presses so it limits the amount of unique combinations a player can perform and a developer can include. However, using a frame-based key press detection system would insure that both scenarios could be satisfied at once. For long combinations like the Konami code and others, gaming systems surely used a frame based approach. However, rather than using a predefined timer triggered by a key press, the timer is triggered by a change to the set of keys and controls the delay between key presses. It is only when this delay is exceeded that the keys are cleared.

    ---Time Passes...---

    Ok so I wrote this four hours ago, and left it open the whole time, referring back to the issues you guys were yapping about. I wrote a new version (not remotely similar to the last) and it seems to work quite well. Bluemagica will be happy to note that this version allows for sequential AND simultaneous key presses in the same combo! Please, bash away guys. (Just focus the swf and press the keys as per the on-screen instructions. 'Keys' are represented by their in-game [aka fighter game] functions.) It should also be noted that while I did not include any actions that use multiple simultaneous key presses, the current build does allow the simultaneous key presses to be checked either way (F+K or K+F, for instance). I did not bother with expanding to loop and try all possible configurations (i.e. 2 skp = 4 checks; 3 skp = 6 checks, 4 skp = 16 checks, and so on... ). Finally, apologies in advance for any miscalculations of actions. I am using the infamously-inaccurate flash.utils.Timer class to check between key presses on a 300ms delay.

    ImprisonedPride's Key-Combo System

    If you wish to see the source you'll have to wait. Not only am I technically supposed to be on vacation, but I have to grab an hour or two rest before I get have to go about my day. I can post the source probably Sunday night (2 class files and the .FLA zipped).
    Last edited by ImprisonedPride; 07-16-2010 at 05:59 AM. Reason: New, sexier demo linked in... with the fonts embedded this time...
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  9. #9
    Senior Member
    Join Date
    May 2009
    Posts
    138
    I wasn't trying to attack your code, just attempting to contribute to the discussion on the concept. The domain you linked to isn't working for me at the moment but your new solution sounds lovely.

  10. #10
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Quote Originally Posted by redjag View Post
    I wasn't trying to attack your code, just attempting to contribute to the discussion on the concept. The domain you linked to isn't working for me at the moment but your new solution sounds lovely.
    I don't take constructive criticism well when I've been up for a day and a half. Also, you're not the only person to have a problem loading it so I uploaded it to megaswf here.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  11. #11
    Member
    Join Date
    Jun 2009
    Posts
    30
    that is one amazing combo system imprisioned pride!, thank you for your help!

  12. #12
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    Basic combo reading system will take a key and check to see if it's the start of any combo
    If it isn't, it performs that key's function
    If it is, it gives the player a few moments to input another key
    If this new key is not part of a combo, it performs the last key's function, and restarts the process with the new key
    If the new key is part of a combo, it restarts the process to see if the combo is the start of any combo, etc.

  13. #13
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    It's a plausible theory, trogdor.

    EDIT: I knew there had to be a hole I just couldn't see.

    Trogdor, if your combo is Punch, Punch, Kick, Kick and I do P,P,K,P, under your theory only the K gets executed but not the two punches. How would you handle it?
    Last edited by ImprisonedPride; 07-19-2010 at 01:32 AM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  14. #14
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    I dished out the source at my blog.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  15. #15
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    No flaw, just whenever you restart the process with a combination in tow, replace the word "key" with "combo"

    So process for exact example executes as such:
    Input P, it is a start of a combo, so allow new input
    Input P (PP), it is still all the start of a combo, so allow new input
    Input K (PPK), still following combo, so still allow new input
    Input P (PPKP), which is not part of a combo anymore, so the PPK that's been stored up gets executed, and awaits for more user input since this new P could be the start of a combo

    However, this gets awkward for long combos
    Most games don't delay your normal actions until it can figure out exactly what you're doing
    Your character will likely punch, punch, kick, and kick, then do their combo, or the last kick is their combo
    Then sometimes it's all a matter of how you want it to work as a game
    Assuming you have combos KK, KKP, and KKPP, do you really want to be able to string all the combos together? Otherwise you'll have to allow for some sort of pause in there, or a different way to choose which one you're pulling off (or just avoid doing such silly combinations altogether)

  16. #16
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Quote Originally Posted by trogdor458 View Post
    However, this gets awkward for long combos
    Which is exactly the flaw I was referring to. My initial thought was that each action should be processed individually but again there is the matter of it looking ridiculous for lengthy combos.
    Quote Originally Posted by trogdor458
    Assuming you have combos KK, KKP, and KKPP, do you really want to be able to string all the combos together?
    Of course not, but my solution intends to allow the player to be so inclined.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  17. #17
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    this code works how i want it to, except for if you press the keys too fast it will fire off the combo check twice. i think it has to do with waiting for the previous key events to finish. any input would be great.

    import flash.ui.Keyboard;
    import flash.utils.Timer;
    import flash.events.Event;

    var KeysList:Array = new Array();

    stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDowns);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);

    var IdleTimer:Timer = new Timer(250);
    IdleTimer.addEventListener("timer", Idle)

    var KeyTimer:Timer = new Timer(251);
    KeyTimer.addEventListener("timer", KeyUpTimer)

    function Idle(e:Event):void
    {
    trace("Idle");
    checkCombo("Idle");
    IdleTimer.stop();
    }

    function KeyUpTimer(e:Event):void
    {
    trace("KeyUpTimer");
    KeysList = new Array();
    KeyTimer.stop();
    }

    function keyDowns(Event:KeyboardEvent):void
    {
    KeysList.push(Event.keyCode);
    trace(Event.keyCode);
    IdleTimer.reset();
    KeyTimer.reset();
    KeyTimer.stop();

    }

    function keyUps(Event:KeyboardEvent):void
    {

    //start idle timer
    IdleTimer.start();
    //start keypress timer
    KeyTimer.start();
    checkCombo("Key up");

    }

    function checkCombo(type):String
    {




    //trace("KeysList " + KeysList);
    trace("CheckCombo " + type);
    //functions for movement based on keyslist{0}
    var MyNumb = KeysList.length;

    //check from the back of the array
    if(KeysList[(MyNumb - 3) ]== 37 && KeysList[(MyNumb - 2)] == 40 && KeysList[MyNumb - 1] == 38)
    {
    trace("*****combo 2****");
    IdleTimer.reset();
    IdleTimer.stop();

    }

    if(KeysList[(MyNumb - 4) ]== 37 && KeysList[(MyNumb - 3)] == 37 && KeysList[MyNumb - 2] == 38 && KeysList[MyNumb - 1] == 40)
    {
    trace("****ultra Super combo**** ");
    IdleTimer.reset();
    IdleTimer.stop();

    }


    if(KeysList[(MyNumb - 3) ]== 37 && KeysList[(MyNumb - 2)] == 37 && KeysList[MyNumb - 1] == 38)
    {
    trace("****Super combo****");
    IdleTimer.reset();
    IdleTimer.stop();

    }


    if(KeysList[(MyNumb - 2)]== 37 && KeysList[(MyNumb - 1)] == 40)
    {
    trace("****combo 1****");
    IdleTimer.reset();
    IdleTimer.stop();
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
    }



    //clear the array if idle fires
    if(type == "Idle"){

    KeysList = new Array();
    }
    return "done";
    }

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