How do I select a random tile in a tilemap?

0 favourites
  • 5 posts
From the Asset Store
A set of retro 16-Bit Neon UI elements to make your menus pop!
  • Exemple:

    So in this case, I need to pick a random tile (every few seconds) around the red tiles and change them to red. and this until there is no more place for red tiles to spread.

    Thank you

    Tagged:

  • One way is to just pick a random tile and check to see if it’s not red and is next to a red tile.

    For example with a 100x200 tilemap you’d get a random tile with:

    set x to int(random(100))

    set y to int(random(200))

    Then the rest is just comparing the tile at a location. It would need to run multiple times to select a tile to fill.

    Now a more direct way could be to loop over all the tiles and store a list of all the valid locations to an array and randomly choosing one of those.

    If you’re clever you could make it so you’d have to loop over everything only once and add and remove from the array as you go. You could try looping up flood fill algorithms to get some ideas too.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So using an array would be the best way. Does this method also works with multiple colour expending? So the same situation but instead of only one red patch there would also be a blue and a yellow one.

    Also how do I do I define "valid locations" since the map will evolve the valiud locations will also change.

  • Also how do I do I define "valid locations" since the map will evolve the valiud locations will also change.

    You will have to refresh the array with valid locations. Use a function to clear and re-populate the array. Call this function every time the map evolves.

  • Here is an example of using the advanced random selection of empty positions in a 2D array.

    cdn.discordapp.com/attachments/225550155531812865/1244194000370532365/RandomPickArrayItem.c3p

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