Jump to content

Move character on tilemap


xyruleib
 Share

Recommended Posts

I have a TileMap I created in tiled. (32x32)
I tried to make the phaser when pressed to the arrow keys a character (sprite) walked to the next tile. But if I do sprite.x + 32, for example, it moves very fast.
Wanted him to walk in tile by tile. Example:
 
 
 if (cursors.left.isDown)    {        // Move to left tile.    }

 

Link to comment
Share on other sites

I'm working on something similar. Here's how I do it, I added comments to explain what's going on.

playerMoving = yes // Check playerMoving at the start of your movement code, only do movement if it is false. If playerMoving is true, it means the player is still moving to his next square.var move = game.add.tween(player) // Create a movement tweenmove.to({x: newX * 32, y: newY * 32}, 180) // Move to the new grid position over a specific duration - this can be set to whatever you like, onComplete will only call when the animation is donemove.onComplete.add(function() {  playerMoving = no // Set playerMoving back to false so that the next movement can start.}, this)move.start() // Now actually run the tween

So basically I use a boolean to see whether the player is moving or not, to only move the player when he's not already moving.

 

Hope this helps you :)

Link to comment
Share on other sites

You have to do manual collision checks, yes. How you do this will depend on how your world data is stored, if it is in your tilemap you can check the tile you're about to move to and see if there's anything on it that could cause a collision. Since your game is grid-based, there won't be many actual collisions, you just check if you're allowed to move to a new tile or not before moving the player.

 

I myself handle my grid differently (I have an object with all tile data separate from the tilemap because it is dynamically loaded and there's more info on it than just the tile), so more samples of my code probably won't help you, but if you're not sure where to start take a look at the "tile properties" example (or the "blank tileset" example), it has some code on finding the tile by X and Y coordinates (it uses the mouse but just substitute player.x and player.y to find the tile you're currently on).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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