How do I do a for each loop regarding a tilemap?

Get help using Construct 2

Post » Sat Jun 07, 2014 11:44 pm

I'm using a tilemap to create my levels, and I'd like to spawn a specific object at certain tiles when the level starts. For example create this object at tilemap "Grass" for the "1,5,6,7" tiles at start of layout. Is there a method of code to do that?
B
68
S
24
G
11
Posts: 452
Reputation: 12,240

Post » Sun Jun 08, 2014 2:43 am

Not 100% sure I understand what you mean, you want a sprite to spawn at a certain tile?

If that's the case you can set the position of the sprite.X to Tilemap.TileToPositionX(TileNumberX) and the same for the Y.

So:

Sprite.X = Tilemap.TileToPositionX(0)
Sprite.Y = Tilemap.TileToPositionY(0)

Would place it in the top left corner.

If you want it to place it at a certain type of tile like grass. You just add a loop that goes through your tilemap and you make a check against the tile that holds the "Grass" one.

What confuse me a little is:
For example create this object at tilemap "Grass" for the "1,5,6,7" tiles at...


Because im not really sure what you refer to, as tile position are a coordinate (0,0) or (2,5) and so on.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Tue Feb 16, 2016 5:40 pm

@nimos100

I think what I'm looking for is similar to ReflextionsDev. If I have a tilemap with grass, rocks, water and also have a tile that is the enemy, could I loop through the tilemap and look for all the tiles with the enemy and spawn my enemy object there?

Reason why I don't just create enemies at X,Y coordinates is because I have the maps generated from JSON file and when creating a level, I can't see where I want to place my enemies (they might be in the walls). If I have a enemy tile, I can just draw my tilemap, export it, and load it in as a level in my game.
Check out my games on Google Play:
BEAST ATTACK (991,300+ downloads!)
ZOMBIE BASH (Android) (Launched Feb 17)
ZOMBIE BASH (iOS) (Launched March 17)
B
51
S
16
G
2
Posts: 428
Reputation: 5,291

Post » Tue Feb 16, 2016 5:53 pm

@jook00 Yes you could, although it quickly becomes inefficient as the tilemap size increases.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Tue Feb 16, 2016 6:19 pm

@Magistross

Yeah, realizing it freezes the game for 5 seconds while it runs through the loop. Figured out how to do it though:

For "X" from 1 to 3000
For "Y" from 1 to 3000
If (Tile loopindex("X"), loopindex("Y")=8)
Then Set tile loopindex("X"), loopindex("Y") to tile 19 Or create enemy
Check out my games on Google Play:
BEAST ATTACK (991,300+ downloads!)
ZOMBIE BASH (Android) (Launched Feb 17)
ZOMBIE BASH (iOS) (Launched March 17)
B
51
S
16
G
2
Posts: 428
Reputation: 5,291

Post » Tue Feb 16, 2016 9:24 pm

I've decided that this method is useless for mobile. It takes nearly a whole minute to run through the loop. If I have more conditions and different objects, it would take a century to run it like this.
Check out my games on Google Play:
BEAST ATTACK (991,300+ downloads!)
ZOMBIE BASH (Android) (Launched Feb 17)
ZOMBIE BASH (iOS) (Launched March 17)
B
51
S
16
G
2
Posts: 428
Reputation: 5,291


Return to How do I....?

Who is online

Users browsing this forum: bluesun66, tarek2 and 2 guests