how do I make a random distribution in the grid?

From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • first of all I'm sorry for my bad english :D

    i want to randomly show different players in a grid.

    i managed to get the players to randomly spawn in the grid.

    but sometimes these appear in the same grid, how can I prevent that


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There are several different ways of doing this. For your purposes, the simplest way might just be to do an extra check after spawning to see if any player objects are overlapping, and if they are, to reassign them to a new square. You'll want to do this in a While loop to ensure you don't randomly put them into a different occupied cell. (This isn't a great idea if you have a very large number of player sprites, but for just 3, this should work just fine).

    Eg. On start of layout:

    -------Spawn Player 1 at random coordinates

    -------Spawn Player 2 at random coords

    -------Spawn Player 3 at random coords

    --------------WHILE Player 1 is overlapping Player 2 OR Player 3

    ---------------------Set Player 1 position to random coords

    --------------WHILE Player 2 is overlapping Player 1 OR Player 3

    ---------------------Set Player 2 position to random coords

  • Thank you!

    did i do something wrong? It doesn't work for me

    the white and blue still overlap the red



    i tried it differently, then the "players" are no longer in the grid

  • Yeah, I'm afraid you haven't quite got that right.

    You've got this:

    Player 3 > Is overlapping Player 2

    Player 3 > Is overlapping Player 1

    Right now, this event only executes if BOTH conditions are true. We don't want that (it's extremely unlikely that all 3 objects will overlap, so this will basically never run) - we want it to run if EITHER condition is true.

    To do that, you need to add "OR". To do this, right-click on the condition block and click "Make 'OR' block".

    That will give you

    Player 3 > Is overlapping Player 2

    OR Player 3 > Is overlapping Player 1


  • You are better off designing it so that the different player objects are never spawned in the same location. This will avoid having to then move them afterwards.

    Have a look at this example:!AkmrWgxeuxlKhIhJtJkjanOQUXHDVA

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