Jump to content

Keyboard Movement Acting Weird


Recommended Posts

So, I implemented keyboard movement into my game:

  .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)

function setup() {

function 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;

Here is the player in it's starting position:


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


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


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.

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.


  • Recently Browsing   0 members

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