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

0 favourites
  • 6 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • 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?

  • 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:

    [quote:2u9wx53u]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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yes you could, although it quickly becomes inefficient as the tilemap size increases.

  • 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

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)