How do I check for a certain tile at any given position

Get help using Construct 2

Post » Tue Nov 07, 2017 9:47 pm

I'm building a level editor, and right now I'm trying to use tilemaps for both terrain tiles and objects (like boxes). I would like to check when the level is tested for box tiles anywhere in the level (by tile id), delete the tile, and replace it with a sprite object instead (so I could make it interactive). If it works properly, I could probably use that method for things like coins, collectibles, doors and stuff.

Also, would it maybe be better to use sprites instead of tilemaps for objects? I just considered that maybe I could maybe assign customizable properties to things like enemies and boxes (properties like health or loot or timers, idk).
B
12
S
4
Posts: 53
Reputation: 1,223

Post » Wed Nov 08, 2017 1:02 am

Quick follow up, one of my events breaks when I leave the initial screen of the layout and move around the camera. I have an event set up so when I click the tilemap in an editor window (separate layer, paralax 0,0), but it selects tile id -1 instead of the tile I clicked on. I'm assuming it's not translating the mouse coordinates to the editor tilemap because of the parallax (the tilemap technically isn't moving). I think this because if I move my camera into the top left corner, I can click on the tiles and select them properly. Is there a way to fix this? mouse position and absolute position both result in the same thing.
B
12
S
4
Posts: 53
Reputation: 1,223

Post » Wed Nov 08, 2017 1:07 am

TileMap object has a number of expressions to convert tile coordinates to screen coordinates and vice-versa.
PositionToTileX(x)
PositionToTileY(y)
SnapX(x)
SnapY(y)
TileToPositionX(x)
TileToPositionY(y)

Use TileAt(x,y) to get tile ID.


As for your second question, it depends on your game. If this is just for storing customizable properties for individual tiles, I would probably use an array or dictionary. If you need to add animations, behaviors etc. to your tiles, you will need to replace them with sprites. (or put sprites on top of them)

larynachos wrote:I have an event set up so when I click the tilemap in an editor window (separate layer, paralax 0,0), but it selects tile id -1 instead of the tile I clicked on. I'm assuming it's not translating the mouse coordinates to the editor tilemap because of the parallax (the tilemap technically isn't moving)..

Not sure I understand.. You may need to add scrollx, scrolly to your mouse coordinates. And probably subtract WindowWidth/2, WindowHeight/2. It's hard to tell without seeing your capx.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
68
Posts: 1,036
Reputation: 37,135

Post » Mon Nov 13, 2017 1:11 am

dop2000 wrote:TileMap object has a number of expressions to convert tile coordinates to screen coordinates and vice-versa.
PositionToTileX(x)
PositionToTileY(y)
SnapX(x)
SnapY(y)
TileToPositionX(x)
TileToPositionY(y)

Use TileAt(x,y) to get tile ID.


As for your second question, it depends on your game. If this is just for storing customizable properties for individual tiles, I would probably use an array or dictionary. If you need to add animations, behaviors etc. to your tiles, you will need to replace them with sprites. (or put sprites on top of them)

larynachos wrote:I have an event set up so when I click the tilemap in an editor window (separate layer, paralax 0,0), but it selects tile id -1 instead of the tile I clicked on. I'm assuming it's not translating the mouse coordinates to the editor tilemap because of the parallax (the tilemap technically isn't moving)..

Not sure I understand.. You may need to add scrollx, scrolly to your mouse coordinates. And probably subtract WindowWidth/2, WindowHeight/2. It's hard to tell without seeing your capx.


Sorry I'm late, it's been a long week. Anyhow, here is what I'm using to select tiles and place them.

Image

tile selector is the tileset the user clicks to choose which block they are placing down. world 1 is the tileset where the user draws the level. the tile selector is on layer 5, which has a parallax of 0,0 (so it remains on screen while the user scrolls around the layout).
B
12
S
4
Posts: 53
Reputation: 1,223

Post » Mon Nov 13, 2017 2:38 am

Try Mouse.X(layer) and Mouse.Y(layer)
Return the position of the mouse cursor in game co-ordinates, with scrolling, scaling and rotation taken in to account for the given layer. The layer can be identified either by a string of its name or its zero-based index

See also this post:
convert-coordinate-from-one-layer-to-another_t71892
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
68
Posts: 1,036
Reputation: 37,135


Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 3 guests