I wanted to update the position of my skspritenodes to push them away from eachother whenever they start to overlap. I was thinking to try using an if statement to check for whether one's frame intersects another, but I am willing to try another solution.

var ships: [SKSpriteNode] = [SKSpriteNode]()
for _ in 1...5 {
        let barge = SKSpriteNode(imageNamed: "BattleShip2")
        barge.setScale(0.1)
        barge.position = CGPoint(x: view.frame.midX, y: view.frame.midY)
        addChild(barge)
        ships.append(barge)
    }

with this initializing loop i end up with 5 objects all stacked on top of eachother in the center of the view. I want them to all seperate from eachother slowly until they are no longer overlapping at all. This is where I'm at:

    var currentShip = SKSpriteNode()
    for ship in ships {
        if ship.frame.intersects(currentShip.frame) {
            // do something
        }
        currentShip = ship
    }
0

There are 0 best solutions below