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

Keyboard Movement - buggy logic #83

Open
ova2 opened this issue Dec 16, 2017 · 3 comments
Open

Keyboard Movement - buggy logic #83

ova2 opened this issue Dec 16, 2017 · 3 comments

Comments

@ova2
Copy link

ova2 commented Dec 16, 2017

Hello,

The code in https://github.com/kittykatattack/learningPixi/blob/master/examples/12_keyboardMovement.html is not quite correct in my opinion. Steps to reproduce:

  1. press the right arrow -> the sprite starts running to the right direction (ok)
  2. keep the right arrow and press the left arrow -> the sprite gets running to the left direction (ok)
  3. release the left arrow -> the sprite keeps running to the left direction (not ok).

The behavior of the step 3) is not correct. The sprite should run to the right because the right arrow is still pressed.

@shamanX86
Copy link

A possible solution,

//assuming the variables for left arrow and right arrow keys to be left and right respectively.

let v1 = 0, v2 = 0, v3 = 0;

left.press = () => {v1 = -5; v3 = -5;}
left.release = () => {v1 = 0;}
right.press = () => {v2 = 5; v3 = 5;}
right.release = () => {v2 = 0;}

//now inside the play(delta) function.

function play(delta) {
    if((v1 + v2 === 0) && v1 != 0 && v2 != 0){cat.vx = v3;}
    else {cat.vx = v1 + v2;}
}

This should bring about the required behavior.

@shamanX86
Copy link

@ova2 can you please check if my solution working for you??

@keanemind
Copy link

keanemind commented Oct 7, 2018

How about if pressing left and right at the same time (or up and down at the same time) cancel each other out, causing no movement along that axis? Many games work this way.

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

Successfully merging a pull request may close this issue.

4 participants