Jump to content

General How to tile-based Movement?


Recommended Posts

So, I made a tile-based system, but it has a bunch of bugs, all due to timing, like if you mash directions or some other funk stuff. I figured maybe you guys could help me understand the logic for how I should be doing it.


Right now, these are the steps that my code goes through:


  • If moving, go forward a pixel amount
  • If no longer moving, find the next tile in the player's direction
  • Stop on the next tile, which is the player's destination


It seems like it should be that simple, but this is the first time I'm trying to make tile-based movement. Right now, it's pixel-based movement until you stop, at which time it will calculate your destination tile. Make sense?


How would I do this properly?

Link to comment
Share on other sites

This is just me sucking at helping without visuals but is there any way you can create a gif/clip of what's going on in your program? The visual aid might give me some clues as to what might not be working.

Link to comment
Share on other sites

well the code suggested here uses both coordinates and tiles as reference and it should find the next tile when the player moves and then the player should move and yeah as panda said visual aid would be nice

Link to comment
Share on other sites

Here is the pseudo code explaining how to set up the keyboard input handler for tile based movement:


if Arrow key pressed then
	if not already moving then
		destination = calculation of current position + direction
		if destination is not blocked then
			player location = destination
			player moving = true
			if direction = up then
				YOffset = -tileheight / 2
			if direction = down then
				YOffset = tileheight / 2
			if direction = left then
				XOffset = -tilewidth / 2
			if direction = right then
				XOffset = tilewidth / 2


All you then have to do is update the YOffset and XOffset over time based on the time it takes the player to walk the tile width / 2 pixels. When both offsets are 0 you've stopped moving and can set player moving to false. When drawing the character on the screen, make sure to add these pixel offsets to the player.


I'm not the best at explaining things in English but I hope the pseudo code I gave you should be sufficient. Any queries feel free to ask.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...