Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onKeyDown stop working for a key when another one is pressed #710

Open
Tokyonono opened this issue Dec 21, 2012 · 4 comments
Open

onKeyDown stop working for a key when another one is pressed #710

Tokyonono opened this issue Dec 21, 2012 · 4 comments

Comments

@Tokyonono
Copy link

This problem can be easily reproduced on the Hello World sample.

Add "this.setKeyboardEnabled(true);" in the init method of a layer.
Add the following method to that layer:
onKeyDown:function(e){
console.log(e);
}

When a key is pressed, the console will show that key in the console at each update (it works for a combination of keys as well). If you keep pressing a key then press another one, only the last one will be shown in the console.

In most of platform games we need to be able to move and press a jump/shoot button at the same time. It can be easily by-passed (keep in memory that a button is pressed and release it on onKeyUp), but it is painful and doesn't seem to be a natural behavior.

@Tokyonono
Copy link
Author

No comment on this? Nobody uses keyboard in video games nowadays?

@Wu-Hao
Copy link
Contributor

Wu-Hao commented Dec 26, 2012

This event fires when a key is being pressed down, and the corresponding keycode is parsed in as a parameter.
You need to keep track of what keys are being held down.

for example, WASD moves a character in 4 directions, When W is keydown, you can record that state in your game logic. then in your character update, check if W is being held down, if so, move up. if W and D is being held down, move up right

@Tokyonono
Copy link
Author

That is what I do, and it works (as I explained in the last sentence of the first message), but it is a by-pass. It should work properly in Cocos2D-html5.
The problem is in your logic:

  • Either "onKeyDown" is called at the moment the button is pressed and then it stops being fired at every update
  • Or "onKeyDown" is fired at every update with the proper list of all currently pressed buttons
    Now we have a mix of both approaches:
  • "onKeyDown" is fired at every update but only with the list of the latest pressed buttons.

The difference between Cocos2D-html5 and the other branches: keyboard matters now.

@Tokyonono
Copy link
Author

PS: I will to provide you with a fix if I have time to work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants