I am trying to add 20 nodes at random locations on the screen without any of the nodes overlapping. Ive got the adding of the nodes at random locations part but I sill get some that overlap. What I have done so far is: I would be greatful for a point in the right dirrection.
while i < 20 {
let bubbleSize = self.frame.width / 12
let bubble = SKShapeNode(circleOfRadius: bubbleSize)
let widthL = -self.frame.size.width / 2 + bubble.frame.size.width / 2 /
let widthH = self.frame.size.width / 2 - bubble.frame.size.width / 2 /
let heightL = -self.frame.size.height / 2 + bubble.frame.size.height / 2 /
let heightH = self.frame.size.height / 2 - bubble.frame.size.height / 2 /
var randWidth = randomNumber(range: widthL..<widthH)
var randHeight = randomNumber(range: heightL..<heightH)
bubble.fillColor = SKColor.cyan
bubble.position = CGPoint(x: randWidth, y: randHeight)
self.addChild(bubble)
}
func randomNumber(range: Range<CGFloat>) -> CGFloat {
//function that gives a random number of a range of CGFloats entered
let min = range.lowerBound
let max = range.upperBound
return CGFloat(arc4random_uniform(UInt32(CGFloat(max - min)))) + min
}