4

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.

Abdou023
  • 1,654
  • 2
  • 24
  • 45

1 Answers1

4

I am sure there are a few different ways to attack the problem and this would be the closest to what you already wrote.

let sprites = [SKSpriteNode]() //loaded with your sprites to spawn
let maxX  = size.width //whatever your max is
let maxY  = size.height //whatever your max is

var spritesAdded = [SKSpriteNode]()

for currentSprite in sprites{

    addChild(currentSprite)

    var intersects = true

    while (intersects){

        let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
        let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY

        currentSprite.position = CGPoint(x: xPos, y: yPos )

        intersects = false

        for sprite in spritesAdded{
            if (currentSprite.intersectsNode(sprite)){
                intersects = true
                break
            }
        }
    }

    spritesAdded.append(currentSprite)
}

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...

var sprites = [SKSpriteNode]() //loaded with your sprites to spawn
var maxX : CGFloat = 0.0 //whatever your max is
var maxY : CGFloat = 0.0 //whatever your max is

var spritesAdded = [SKSpriteNode]()

override func didMoveToView(view: SKView) {
    maxX  = size.width //whatever your max is
    maxY  = size.height //whatever your max is
}

func addSprite(){

    if let currentSprite = sprites.first {

        let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
        let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY

        currentSprite.position = CGPoint(x: xPos, y: yPos )

        for sprite in spritesAdded{
            if (currentSprite.intersectsNode(sprite)){
                return
            }
        }

        addChild(currentSprite)
        spritesAdded.append(currentSprite)
        sprites.removeFirst()
    }
}

override func update(currentTime: NSTimeInterval) {
    addSprite()
}

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.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
  • The memory issue happens in my or your function or both ? I only use this method once at the start of the scene, will that have a major effect on performance ? – Abdou023 Oct 12 '15 at 14:19
  • @Abdou023 It isn't a memory issue but a potential performance issue. If you try to add them all at once on load you may get different load times based on how many times it has to get a new random position. If you have plenty of room on screen/lots of possible locations it isn't a big of a deal. – Skyler Lauren Oct 12 '15 at 14:41
  • Ok, after using the function I found that the game freezes after few rounds, I did some debugging and the problem is most likely is in the for loop inside the while loop. Keep in mind that the entire game runs in a single scene, when the round ends a main menu appear inside the same scene where the game runs. – Abdou023 Oct 14 '15 at 20:22
  • @Abdou023 You will need to clear out the spritesAdded array as you are removing them from the scene. Guessing that is where you are running into issues. – Skyler Lauren Oct 14 '15 at 21:27