How do I make the player only able to place object on a grid

Get help using Construct 2

Post » Tue Feb 28, 2017 11:20 am

Hi, I am making a completely shameless Terraria/Starbound ripoff (eventually it will be less shameless, I promise) and something I can't figure out is how to make the player only able to place blocks on a grid. I've figured out how to constrain their reach using a line of sight behavior on the player's hitbox object, but they can still be placed anywhere in space instead of snapping to the same 32x32 grid the tiles are on. I'm sure there is a solution here but I need your help to find it!
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Tue Feb 28, 2017 1:19 pm

If you use a tilemap, and you should, then the tilemap has expressions to help you.

PositionToTileX(positionx)
PositionToTileY(positiony)
Convert an X or Y layout co-ordinate in to the corresponding tile number in the tilemap. For example, this can be used to get the tile position under the mouse.

SnapX(positionx)
SnapY(positiony)
Snap an X or Y layout co-ordinate to the nearest tile. This also returns a layout co-ordinate, but aligned to the nearest tile in the tilemap.

TileToPositionX(indexx)
TileToPositionY(indexy)
Convert a tile position to layout co-ordinates. For example, this can be used to position a Sprite object on top of a given tile.


Using snap.
When a box is placed.
Set the position of the box to
x = tilemap.SnapX(box.X)
y = tilemap.SnapY(box.Y)

Using the other two.
When a box is placed.
Set the position of the box to
x = TileToPositionX(PositionToTileX(box.X))
y = TileToPositionY(PositionToTileY(box.Y))

If you dont use a tilemap.
When a box is placed.
Set the position of the box to
x = (round(box.X/32)) * 32
y = (round(box.Y/32)) * 32
But, that will not be exact.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue Feb 28, 2017 7:50 pm

That's very helpful, thank you! :D
B
18
S
9
Posts: 103
Reputation: 1,817


Return to How do I....?

Who is online

Users browsing this forum: Apollo14 and 25 guests