0

I'm trying to spawn multiple SKSpriteNodes within the set location, but when I add multiple nodes they just spawn on top of each other? Is there a way to not make this happen?

I'm also having a problem of having to refresh the page in order to get a new location for the node. Is there a way so if the node disappears on the page it will spawn in a new location within the set co-ords?

let rect = CGRectMake(x: 90, y: 360, width: 200, height: 200)
let x = rect.origin.x + CGFloat(arc4random()) % rect.size.width
let y = rect.origin.y + CGFloat(arc4random()) % rect.size.height
let randomPoint = CGPointMake(x, y)
self.redcircle.position = randomPoint
self.addChild(redcircle)
self.bluecircle.position = randomPoint
self.addChild(bluecircle)
Ben Kane
  • 9,331
  • 6
  • 36
  • 58
MattJB
  • 13
  • 6

2 Answers2

0

You need 2 randomPoint's to put them on a another place. You use 2 Nodes with one Position cause randomPoint is always the same.

Edit(question in comment):

You have to use min(x, y) and max(x, y)

    let x = random(CGRectGetMinX(self.frame), max: CGRectGetMaxX(self.frame))
    let y = random(CGRectGetMinY(self.frame), max: CGRectGetMaxY(self.frame))

let randomPoint = CGPointMake(x, y)
self.redcircle.position = randomPoint
self.addChild(red circle)

And here the random function:

func random() -> CGFloat {
        return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
    }

    func random(min: CGFloat, max: CGFloat) -> CGFloat {
        return random() * (max - min) + min
    }
Lirf
  • 111
  • 12
  • Thank you! Do you know what I can do to spawn the node in a different place without backing out of the screen. So I could use the SKAction to fadeOut and it would reappear in a different location? – MattJB Jul 28 '15 at 11:58
  • Edited, you need min and max of the screen – Lirf Jul 28 '15 at 12:37
0

It would be easier for you to make a method which generates random point based on given rectangle. Or method which spawns sprite within given rectangle, like this:

import SpriteKit


class GameScene: SKScene {

   let rect =  CGRect(x: 90, y: 360, width: 200, height: 200)

    override func didMoveToView(view: SKView) {


        let debug = SKSpriteNode(color: SKColor.redColor(), size: rect.size)

        debug.alpha = 0.2

        debug.position = rect.origin

        let origin = SKSpriteNode(color: SKColor.redColor(), size:CGSize(width: 5, height:5))

        debug.addChild(origin)

        self.addChild(debug)

    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

       let sprite = spawnSpriteAtRandomPositionWithinRect(rect)


       self.addChild(sprite)


       println("Sprite spawned at position x,y( \(sprite.position.x), \(sprite.position.y))")
    }

    func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{

        return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
    }

    func spawnSpriteAtRandomPositionWithinRect(rectangle:CGRect)->SKSpriteNode{


        let x = randomBetweenNumbers(rectangle.origin.x - rectangle.size.width / 2.0 , secondNum: rectangle.origin.x + rectangle.size.width/2.0)
        let y = randomBetweenNumbers(rectangle.origin.y - rectangle.size.height / 2.0 , secondNum: rectangle.origin.y + rectangle.size.height/2.0)

        let sprite = SKSpriteNode(color: SKColor.greenColor(), size:CGSize(width: 30, height: 30))

        sprite.position = CGPoint(x: x, y: y)


        return sprite
    }

}

Sprite named debug is not needed actually, but it shows you visually given rectangle.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • It says I cant put let sprite = spawnSpriteAtRandomPositionWithinRect(rect) there as it is before is declaration? – MattJB Jul 28 '15 at 15:27
  • @MattJB I am not getting any error with the code above. Can you copy & paste the real error message ? – Whirlwind Jul 28 '15 at 18:17
  • I tried the code in a new file within the app, but it docent work with the other code i have on the file, is there a way to make that whole code a fun so i can add it in via a new file? – MattJB Jul 28 '15 at 18:41