J Norris J Norris - 3 months ago 8
Swift Question

add Sprite if certain score is reached

I have a simple game. You start with a ball and you bounce it until you get 10 points. When you reach 10 points, another ball appears(so that reaching the next landmark can be faster). When 20 points is reached, a third ball appears. I have this already set up, but I need to know how to do an

if
statement with
case
.

This is the statement I want to implement.

if (score == 10) && secondball.parent == nil {


This is what I have so far:

import SpriteKit

class GameScene: SKScene {
override func update(currentTime: CFTimeInterval) {


guard shouldUpdateTexture else { return }
if shouldUpdateTexture {
shouldUpdateTexture = false
switch score {
case 10...19: backgroundNode.texture = SKTexture(imageNamed: "orangebackground")

let secondball = Ball()
secondball.position = CGPoint(x:self.frame.midX * 1.65, y: 440)
addChild(secondball)



secondball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
secondball.physicsBody?.dynamic = true
secondball.physicsBody?.allowsRotation = false

secondball.physicsBody?.friction = 0
secondball.physicsBody?.angularDamping = 0
secondball.physicsBody?.linearDamping = 0
secondball.physicsBody?.usesPreciseCollisionDetection = true
secondball.physicsBody!.categoryBitMask = 0



case 20...29: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")

let thirdball = Ball()
thirdball.position = CGPoint(x:self.frame.midX * 0.35, y:440)
addChild(thirdball)



thirdball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
thirdball.physicsBody?.dynamic = true
thirdball.physicsBody?.allowsRotation = false

thirdball.physicsBody?.friction = 0
thirdball.physicsBody?.angularDamping = 0
thirdball.physicsBody?.linearDamping = 0
thirdball.physicsBody?.usesPreciseCollisionDetection = true
thirdball.physicsBody!.categoryBitMask = 0

case 30...39: backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
case 40...49: backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
case 50...59: backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")
case 50...59: backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")

let secondball = Ball()
secondball.position = CGPoint(x:self.frame.midX * 1.65, y: 440)
addChild(secondball)



secondball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
secondball.physicsBody?.dynamic = true
secondball.physicsBody?.allowsRotation = false

secondball.physicsBody?.friction = 0
secondball.physicsBody?.angularDamping = 0
secondball.physicsBody?.linearDamping = 0
secondball.physicsBody?.usesPreciseCollisionDetection = true
secondball.physicsBody!.categoryBitMask = 0


The
case:10...19
works and so does the
case:20...29
, but for
case 50...59
I only want another ball to show up if the previous one isn't there.

If you need any more or any less information, just comment below. I'll update it immediately.

Thanks, Jordan.

Nik Nik
Answer

In the case 50...59: simply add an if statement that checks if the ball is already there. Add something like this:

if thirdBall.parent != nil {

addChild(thirdBall)
// Whatever other code you want to run only if the ball needs to be added to the scene

}

Hope this helps!

Comments