Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Help with movement in javascript [noob]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 08 January 2013 - 02:37 PM

ok, I am very new to programming so it may sound like a stupid question but I can't work it out.

 

So, with the help of a book, I made an animation in javascript with html5, basically there is and animation loop that constantly increases the variables that store the data of shapes positions. I understand it all, but I want to make it so the shapes only move when I am pressing buttons on my keyboard, so I can move a shape around the screen. 

 

I think I just need to put an if loop around the bit where I increase the x and y positions with the condition that a certain button is pressed. I don't know how to detect if a button if pressed and was hoping for an easy solution that is only a few lines of code.

 

Also, if you can't answer the question directly, are there any good websites/tutorials that do, I googled it but didn't find anything useful.



Sponsor:

#2 Jutaris   Members   -  Reputation: 478

Like
2Likes
Like

Posted 08 January 2013 - 04:49 PM

Handling keyboard events in JavaScript is fairly straightforward. 

 

Not sure if your using any libraries or what your code looks like, but I suspect the information in this blog post should help get an understanding of how to do what you're after, specifically this portion:

 

window.addEventListener('keydown', function(event) {
  switch (event.keyCode) {
    case 37: // Left
      Game.player.moveLeft();
    break;

    case 38: // Up
      Game.player.moveUp();
    break;

    case 39: // Right
      Game.player.moveRight();
    break;

    case 40: // Down
      Game.player.moveDown();
    break;
  }
}, false);

 

Hope that helps. :)


My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

Come join us for some friendly game dev discussions over in XNA Chat!


#3 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 10 January 2013 - 01:12 PM

Handling keyboard events in JavaScript is fairly straightforward. 

 

Not sure if your using any libraries or what your code looks like, but I suspect the information in this blog post should help get an understanding of how to do what you're after, specifically this portion:

 

window.addEventListener('keydown', function(event) {
  switch (event.keyCode) {
    case 37: // Left
      Game.player.moveLeft();
    break;

    case 38: // Up
      Game.player.moveUp();
    break;

    case 39: // Right
      Game.player.moveRight();
    break;

    case 40: // Down
      Game.player.moveDown();
    break;
  }
}, false);

 

Hope that helps. smile.png

Yh thats was extremely helpful, I got it working, thanks!



#4 Jutaris   Members   -  Reputation: 478

Like
1Likes
Like

Posted 10 January 2013 - 02:19 PM

Happy to help!


My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

Come join us for some friendly game dev discussions over in XNA Chat!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS