Jump to content

Keyboard Movement Acting Weird


OhNoItsATornahdo
 Share

Recommended Posts

So, I implemented keyboard movement into my game:

PIXI.loader
  .add(rooms[0].imageName, rooms[0].image)
  .add(rooms[1].imageName, rooms[1].image)
  .add(rooms[2].imageName, rooms[2].image)
  .add(player.imageName, player.sprite)
  .load(setup);

function setup() {
  loadRoom(0);
  animationloop();
}

function animationloop() {
  requestAnimationFrame(animationloop);
  document.addEventListener('keydown', function(event) {
    if (event.keyCode == 68) {
      player.x += 1;
    } else if (event.keyCode == 65) {
      player.x -= 1;
    }
    if (event.keyCode == 87) {
      player.y -= 1;
    } else if (event.keyCode == 83) {
      player.y += 1;
    }
  });
  renderer.render(stage);
}

Here is the player in it's starting position:

2017-09-02-12-32-localhost-8080.png.c91c63cd9c780b308cbcedbc4f0830f0.png

Then I press (technically, I tapped) 'D':

2017-09-02-12-33-localhost-8080.thumb.png.f297df788b0f9c91b28ca456e0ca9fe3.png

So everything looks okay right now, but if I press 'D' once again:

2017-09-02-12-34-localhost-8080.thumb.png.f82ed0a54498a4624d32496b62ddad01.png

How did my player go from the side of the bed to the front of the computer in one tap? It just keeps going; my player teleports farther and farther as I press 'D' or any other button that moves my player.

 

Could someone please help me?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...