I want to create some sprites at random positions without getting overlapped, here is my code :
var sprites = [SKSpriteNode]()
for index in 0...spriteArray {
let sprite = SKSpriteNode(imageNamed: named)
sprites.append(sprite)
checkInterception(sprite, sprite2: sprites)// positioning using a function
addChild(sprite)
}
Here I check for overlapping:
func checkInterception(sprite1: SKSpriteNode, sprite2: [SKSpriteNode]) {
let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY
sprite1.position = CGPoint(x: xPos, y: yPos )
for index in 0...sprite2.count-1 {
if sprite1.intersectsNode(sprite2[index]) {
let yPos = sprite1.position.y + sprite1.size.height
sprite1.position = CGPoint(x: xPos, y: yPos )
}
}
}
But some sprites still get overlapped. I know there is something not right with the for loop, but just can't figure it out.
I am sure there are a few different ways to attack the problem and this would be the closest to what you already wrote.
Things to consider is if you don't already know where it is safe to add a sprite you run the risk of performance. For instance adding your last sprite may take 1,000,000 attempts before it randomly picks a point that doesn't intersect with others.
If you don't have to have them all added at once I would attack the problem on the update loop and do something like this...
Kind of rough but the idea is every update it will try to add a sprite. This way if you run out of room it won't lock up. Hopefully that helps.