Jump to content

How to check my sprite can move


useryaha
 Share

Recommended Posts

Hello,

I'm sorry for my english, I speak french.

I am trying to create a game with Phaser v2.3.0, but I need help for a problem.

In my game, the player create a new impassable sprite when he move, and he move on a grid.
But, the player can be blocked by these new impassables sprites.

When the player is blocked, I want to stop the game.
How to check the player can move or not?
He can be blocked by impassable sprites, or tiles from json tilemap.

"player.body.blocked" and "player.body.touching" don't work, because the player don't really touch others sprites.

 

image-phaser.jpg

 

Here, the player (the green slime) goes to top, and he create a impassable puddle.

After, how to detect that the player is blocked ?

 

I tried looping for each sprites puddles, and see if they surround the player with X and Y coordinates. But I don't know looping tiles (grey wall) of my layer (from JSON tilemap).

 

Do you have any advice?

Thanks.

Link to comment
Share on other sites

I would store this information in a JavaScript array and don't rely on Phaser itself to check if the sprite is there for you. It depends on how your game works, but if all those objects are in a grid and the player can only move along the same grid then it's easier to *not* use physics do accomplish this.

Link to comment
Share on other sites

Thank you for your answers.

@Carlos, this does not work, I think "player.body.blocked" and "player.body.touching" works only when the sprite in moving.
But in my case, when I want to know my player is blocked when he is stopping...

@drhayes, you are probably right, but I want to use a "TILED_JSON" tilemap to build my level, with the functions of phaser.
I can easily detect the puddles around my player with a javascript array, but how I can to loop through the tiles a specific layer of my tilemap?
If I can browse a table with the X and Y coordinates of each tile of my layer, I think I would succeed.

My level is built like this:

game.load.tilemap("level-1", "src/levels/level-1.json", null, Phaser.Tilemap.TILED_JSON);//...levelMap = game.add.tilemap("level-1");levelMap.addTilesetImage('wall');wallLayer = levelMap.createLayer('walllayer');wallLayer.resizeWorld();        levelMap.setCollisionBetween(1, 12);

How can I to loop the tiles of "wallLayer" ?

Link to comment
Share on other sites

You've got a couple of options here. The layer that has the puddles is a 2d array at its heart. The TILED_JSON format stores what it calls the GID (global id) of each tile in the array. The value of the GID depends on the order you added the tilesets to the map, so you'd have to add them in the same order every time to get the same GID or check the tileset name of each tileset and find the GID that way.

 

You can also call tilemapLayer.getTiles to retrieve a range of tile objects.

 

I don't know which one is better for you than the other. In the game I'm working on now, every map has the same tileset loaded first so that the GIDs are the same across all my tilemaps. I then use those tiles as a collision layer that I set "visible = false" when loading the map. I have a solid tile and a one-way collision tile in there right now. Maybe this is a dumb way of doing it but it worked for me. ( =

Link to comment
Share on other sites

It is a good idea, and tilemapLayer.getTiles() function is perfect for me.
I created a 2D array with a boolean value for each squares of my map, to define the impassables obstacles (the tiles of my "wallLayer").
Then, I can easily change a value when the player create an impassable puddle.

Thank you !

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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