How do I Spawn Sprites on TileMap

0 favourites
  • 9 posts
From the Asset Store
Build your map with these isometric objects and terrains
  • Hello fellow constructors!

    Does anyone know how I could spawn sprites on top of certain tiles of a tilemap?

    For example, let's say the tile with IID=1 would be a tile on top of which I would like to spawn Sprite1...I am trying to spawn things on start of the layout as follows:

    System on start of layout:

    TileMap.IID = 1

    and as action: Create object at Sprite1 on layer 0 at (TileMap.PositiontoTileX(TileMap.IID=1), (TileMap.PositiontoTileY(TileMap.IID=1)

  • You have to do like this:

  • Thanks for the quick response. However, I need a bit more advanced solution - IID in tilemap points to a certain kind of tile, as far as I have understood it. I would like to be able to create a sprite on top of all certain kinds of tiles. I will include screen capture to illustrate this:

    In this example, I am trying to create the sprite on top of all cactus tiles ( IID=15 ). How should I approach this?

    I tried also replacing IID with UID, but it doesn't seem to do any difference.

  • Ahh I see you cant do like that, the IID and UID doesn't have anything to do with the tiles, it have something to do with the Tilemap object it self. What you would do is like this then.

  • Thanks! This is exactly what I was looking for =) No wonder I wasn't able to make it working, I thought IID / UID could be used to point out to certain tile in the tileset.

    Have a great evening! =)

  • Tile id and Tilemap.IID are two completely different things

    If you need to place sprite on every tile with id=15 you need to use For loop

    something like

    +For "x" from 0 to tilemap.width/"cellsize"

    +For "y" from 0 to tilemap.height/"cellsize"

    ++ Tilemap: Tile (loopindex("x"), loopindex("y") = 15 - > System: Create object Sprite at (Tilemap.TileToPositionX(loopindex("x")), Tilemap.TileToPositionY(loopindex("y")))

    Edit: yep, ninjad by nimos100

  • Thanks also for your effort shinkan =) great to see such an active community around this wonderful tool

  • Can see you are a bit confused about IID and UID and how to do it, so here is how you do it correct no matter how many cactus you have and how big you tilemap is, with a better explanation.

    IID and UID:

    Are not something that are unique for tilemaps. In fact all objects you add to your project will be assigned these. My best advice for now just to completely forget IID and only think of UID, I have used C2 for quite a long time and have yet to run into any problem where IID would be useful.

    UID is a "Unique identification number" (I think that what it stands for) but even if it doesn't you can think of it like that. Its different for all objects so you will never have any sprite, tilemap, button etc with the same number. This make UID very good for picking things, because you know it will always only pick one.

    If you look at the screenshot below of the correct code for exactly what you want to do.

    The loops are nested, which is the normal way of going through a lot of elements like in a tilemap or an array.

    If you look at the tilemap, the tile in the top left corner have a position of 0,0 the one to the right is 1,0 next 2,0 and so on.

    Since the program doesn't know where in the tilemap we have placed the cactus, we need some way to let it know. So we use loops to go through each tile to see if its a cactus. Since we don't know where to start we "set" the position to (X,Y) by default (You wont actually do this.)

    The first loop "Tile X" will start at 0.

    So now it would be (0,Y)

    Since the next loop is a sub event to the first loop and goes through the Y and also starts at 0, we get. (0,0)

    So it checks if (0,0) is equal to 15, which in this case it ain't so It loops again. However since "Tile Y" loop are the last one it will continue this one first before going back to the "Tile X" loop.

    So next time it will be (0,1),(0,2),(0,3).........until it reach the end of Y. In this case I have set it to int(Tilemap.Height / 32), this is because this will always give us the correct number of tiles to check in Y, the reason I divide with 32, is because each tile is 32x32 and int simply does that we always get an integer as you cant be sure that the tilemap height is always an integer if you scale it and then divide it with something, but the loop requires an integer to work or will go into an endless loop, which will make you have to force close the program, so that's not good, but happens quite a lot.

    So if the tilemap is 128 pixels in height we divide it with 32 and get 4.

    So the loop will run from 0 to 4, so in fact if we were to be 100% accurate we could subtract 1 from it as the tilemap starts at 0. So it should actually only run from 0-3 as the loops include both 0 and 3.

    But anyway when it reaches the end of "Tile Y" loop it will go back to the "Tile X" loop and increase that one, so now you would get


    And it would go into the "Tile Y" loop again and it would then be (1,0) and then keep doing that until also X reaches it limits.

    Hope that didn't confuse you

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the thorough explanation nimos, I'm fairly confident to go onwards now with my project haha

    I've been playing around with construct on and off in the past months, and I recently bought a personal license with the idea of doing few hobby projects with it.

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