Question

I am developing a game with a large amount of code. The unfinished version of the game can be found here: http://rainisfalling.co.za/sheep-jump-test/

There are two Key Listeners. One listens for SPACEBAR for the big jump, the other listens for CTRL for the small jump. The problem I am experiencing is that when the two buttons are pressed precisely the same time, both jump actions occur, resulting in a super big jump. (A combination of the two jump heights.)

Here is a simplified version of my code:

addEventListener(KeyboardEvent.KEY_DOWN, bigJump);

function bigJump(e:KeyboardEvent){
//check to see that keycode = SPACEBAR
//code to do the actual jump
//also remove the event listeners for the jumps while in the air
}


addEventListener(KeyboardEvent.KEY_DOWN, smallJump);

function smallJump(e:KeyboardEvent){
//check to see that keycode = CTRL
//code to do the actual jump
//also remove the event listeners for the jumps while in the air
}
Was it helpful?

Solution

Combine jumps into one handler:

addEventListener(KeyboardEvent.KEY_DOWN, jump);

function jump(e:KeyboardEvent){
       switch( e.keyCode ){
       case 32:  //<Space>
       //Big jump code
       break;
       case 17:  //<Ctrl>
       //Small jump code
       break;
       }
}

OTHER TIPS

This is probably going to seem really obvious after you read it, but just add one listener within the function do if(CTRL) smallJump else if(SPACEBAR) bigJump

This way only one condition happens... also consider leaving it in and integrating that in to the game, I like the super jump idea :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top