Fixing buggy movement

Started by
2 comments, last by JackBid 11 years, 2 months ago

Working on just having smooth movement of a square in javascript. At the moment it is very choppy and not at all smooth. This is what I am using at the moment:


window.addEventListener('keydown', function(event) {
     if(!dead && playing){
 	switch (event.keyCode) {

 	case 37: 
 	moveLeft = true;
 	break;
 
   	case 38: 
     	moveUp = true;
   	break;

   	case 39: 
   	moveRight = true;
   	break;
 
    	case 40: 
   	moveDown = true;
    	break;
  	}
  	};
	}, false);

     function GameLoop(){

	if(moveLeft){
	x -= 6;
	moveLeft = false;
	}else if(moveUp){
	y -= 6;
	moveUp = false;
	}else if(moveRight){
	x += 6;
	moveRight = false;
	}else if(moveDown){
	y += 6;
	moveDown = false;
	}

     if(playArcade){
        setTimeout(GameLoop, 33);
     }

I read this blog post about on the topic of smooth movement in javascript: http://nokarma.org/2011/02/27/javascript-game-development-keyboard-input/index.html

However, I did not understand a lot of it, although I think it contains the answer to my problem. If some could explain what the blog post says/means to a noob like myself that would be great - or find another solution to my problem.

p.s. I may have missed some of the detail in the code posted above, if some of the variables and other items are missing, its just because I did not post them in this code, they are in the real thing though.

Advertisement

I'm not sure what you mean by smooth movement, but I would remove the move___ = false; statements so that you don't have to keep pressing the buttons over. Once you do this, you would have to add another event listener for 'keyup' and set the move___ = false when that happens. This way, as long as you're holding down a direction, the gameloop will update your position, but as soon as you let go, it will stop. If you do it this way, you can change the 6 to a smaller number like 1 to achieve a less "jumpy" motion.

Also, typically you'd want to keep track of your delta_time in the game loop (how much time has passed since the last run of the loop).

Then to update position, you would add (velocity * delta_time) to your position.

I don't think I can explain it better than the article does:

This first version kinda works, but it is pretty choppy and only ever works for one key. Why?

The choppiness is due to the fact that the keydown event is fired repeatedly with small pauses between each event, and these pauses are not synced up with our game update code. So we’ll have to figure out a way to get them in sync.

The problem that only ever one key is recognized comes from the fact that the browser only ever repeats keydown events for the last key that was pressed.

For working around these two problems, I’m using this super simple helper object:

And the rest of the article explains the solution.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I'm not sure what you mean by smooth movement, but I would remove the move___ = false; statements so that you don't have to keep pressing the buttons over. Once you do this, you would have to add another event listener for 'keyup' and set the move___ = false when that happens. This way, as long as you're holding down a direction, the gameloop will update your position, but as soon as you let go, it will stop. If you do it this way, you can change the 6 to a smaller number like 1 to achieve a less "jumpy" motion.

Also, typically you'd want to keep track of your delta_time in the game loop (how much time has passed since the last run of the loop).

Then to update position, you would add (velocity * delta_time) to your position.

Thank you, I think the solution was just checking when the key had been released. Saved me loads of time, thanks again :) !

This topic is closed to new replies.

Advertisement