Jump to content

preventing randomly placed sprites in a group from overlap


fivesenses99
 Share

Recommended Posts

I am trying to figure out a way to prevent randomly placed sprites without a group from overlapping. For instance the following code will create randomly placed sprites, but is there a feature of phaser than can help me prevent overlap in such a case? 

Thanks in advance

 

function create() {   sprites = game.add.group();    for (var i = 0; i < 30; i++)    {        var s = sprites.create(game.rnd.integerInRange(0, game.world.width - 32), game.rnd.integerInRange(0, game.world.height - 32), 'diamond');    }}

 

Link to comment
Share on other sites

Arcade bodies have a property called "embedded" that looks like it'll help: "If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true."

 

I've never used it, though, so I'm not sure.

Link to comment
Share on other sites

what alex_h said... you don't want to get stuck in a loop where you're trying to randomly place an object and if it overlaps you randomly place it again, only to find it's overlapping again .. could happen, especially the more objects you have.

 

you're going to need to keep track of them manually somehow.. grid cells sounds good to me

Link to comment
Share on other sites

Or you could have the coordinates being some modulo of the sprite's width /height (like {20, 20}, {40, 40}, {60, 60} if your sprite is {20, 20}) and you store the already distributed coordinates, not to give them again. It's a lot like a grid.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...