cuber
cuber

Reputation: 371

Choosing to spawn different SKSpriteNodes

I am making a game and I have objects which fall from the top of the screen to the bottom. I want to spawn choose between the objects and drop one of them. I currently the drop all at the same time.

 func ShapePicker() -> SKSpriteNode{
    let shapeArray = [purpleOctagon, coin, greenTriangle, orangeHexagon]

    let MaxValue = self.size.width / 2 - 200
    let MinValue = self.size.width / 3 * 0.95

    let rangeMax = UInt32(MaxValue)
    let rangeMin = UInt32(MinValue)

    purpleOctagon.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
    self.addChild(purpleOctagon)

    greenTriangle.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
    self.addChild(greenTriangle)

    coin.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
    self.addChild(coin)
    return shapeArray[Int(arc4random_uniform(UInt32(shapeArray.count)))]

}

I would like the program to randomly .addChild because right now it just puts them on the screen.

Upvotes: 1

Views: 124

Answers (2)

Fluidity
Fluidity

Reputation: 3995

Your code implies that you want all of them to be on the screen, and then one randomly drops... So you do want to continue to .addChild. What you want, is for them to NOT drop all at once.

So, you need to change the .physicsBody.pinned to true to keep them at the top of the screen.

Then, in your update() you can check for how much time has passed, etc, and after a certain # of seconds you can do an arc4random_uniform and use that result to change the .pinned property of one of the nodes (thus causing that one and that one only to fall).

So, if the coin is 0, triangle is 1, and octagon is 2, then, in your .update keep track of the time elapsed and after say 3 seconds, do a random check 0-2, and that check will perform:

switch result {
  case 0: // coin
    coin.physicsBody?.pinned = false // makes it drop
  case 1: // triangle
  ...

Just make sure that your nodes are in the proper scope so that you can do the logic in update()

If I read your Q wrong, and you only want to spawn and then drop just one, then you would still need the above switch statement, but instead of changing physicsBody you would do .addChild instead.

so inside of your func would be more like:

// This can be global or inside of your GameScene:
var myGlobalCurrentTime: CFTimeInterval 

override func update(currentTime: CFTimeInterval) {

  myGlobalCurrentTime = myTimerUpdateTime(currentTime)

  func myDropFunc() {

    ... // Initialize your nodes

    let result = myRandomNumber(3)

    switch result {
      case 0:
        coin.position
        = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax),
                  y: self.size.height)
        self.addChild(coin)
      case 1:
        ...
    }
  }

  // Execute the func:
  myDropFunc()
}

I'm still a bit confused by your code and question, so please clarify in the comments so I can update this answer if needed.

Upvotes: 1

Chris Slowik
Chris Slowik

Reputation: 2879

You can move the addChild call out of this function, it returns a SKSpriteNode, which you can then add.

let sprite = ShapePicker()
addChild(sprite)

Or, just add the one randomly chosen and return it. You don't need to addChild nodes that you're not planning on using

Upvotes: 0

Related Questions