Working with Tiles in Tilemap: Count amount of Tiles



Well that is a question I asked myself and when looking for a reference I realized that there is no tutorial or reference with a solution to this question (at least San Google did not find it).

This problem was presented to me by doing a simple ARCANOID style game and wanting to know how many TILES I drew inside a TILEMAP object and take advantage of the TILEMAP object's characteristic to do the STAGES, to which the doubt assailed me:

** How many TILES did I draw so that when I finished destroying them, the game went on to the next stage?

With several stages already ready and with no desire to start counting the TILES one by one, I wanted CONSTRUCT 2 to do the job.

We must know that no matter what mechanics we do, this will work for any TILEMAP that we have in our game.

1.- We must create 2 objects 1 TILEMAP and 1 Text. Our Tilemap for the example will be called "STAGES".

2.- Add the image with which you will work within the Tilemap.


3.- Create an Instance Variable within the Tilemap.

In this case, "CBLOQUE" will keep track of how many Tiles the corresponding Tilemap has active.

4.- We will start creating an EVENT ON START OF LAYOUT and a FOREACH set as SubEvent and with this we select from TILEMAP (0) .... to the TILEMAP (n).

Where the SENTENCE IS 0 and the Order: Ascending.

5.- We created a SubEvent of the FOR System to count VERTICALLY, here it is IMPORTANT to know the TIL ALT (not the TILEMAP), for the example that the HIGH OF EACH TILE IS = 8. To know how many TILES fit in a VERTICAL we divide the HEIGHT of the TILEMAP by the HEIGHT of the TILE .... with ETAPAS.Height / 8.

This FOR is called "CONTVERT".

6.- We created a System SubEvent FOR to count HORIZONTALLY, here it is IMPORTANT to know the Tile WIDTH (not TILEMAP), for the example I know that the WIDTH of EACH TILE IS = 16. To know how many TILES fit HORIZONTAL we divide the TILEMAP WIDTH by the TILE WIDTH .... with ETAPAS.Width / 16.

This FOR is called "CONTHOR".


7.- Here the good starts ... this part is what makes the magic.

Again add a SubEvento System "COMPARE TWO VALUES", which will be responsible for searching with "TILEAT" of the TILEMAP and within the "LOOPINDEX": "CONTVERT" AND "CONTHOR" and compare if it is empty (Value "-1") or full (different or greater than "-1"), making a small variation we can count any number "INDICE" TILE (Example: Only the "0" or "10", to know how many of each type).

It would be like this: STAGES.TileAt (loopindex ("CONTHOR"), loopindex ("CONTVERT"))

8.- Once all these EVENTS are done, we create an ACTION that adds 1 to the variable CBLOQUE, when it finds a value different to "-1" inside the TILES spaces of TILEMAP and that represents it in a TEXT.

And that's it, we already know how many TILES we put in and we can compare how many were destroyed or generate some action based on CBLOQUE.

Final example:

Here we have 1 TILEMAP copied 1 time and with different TILES in both INDEX and quantity.

We superpose them ...

And when executing ...

I hope this Tutorial is clear and easy to understand and above all will help you.


  • Order by
Want to leave a comment? Login or Register an account!