Reputation: 23
class GameScene: SKScene {
let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png"),
]
override func didMove(to view: SKView) {
spawnBalls()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for ball in balls{
ball.physicsBody = SKPhysicsBody()
ball.physicsBody?.affectedByGravity = true
}
}
func spawnBalls() {
for ball in balls{
balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: 0, y: 250)
ball.size = CGSize(width: 70, height: 70)
self.addChild(ball)
}
}
}
Every time my app loads the red ball spawns but it is supposed to randomly spawn either the red, blue or green ball. At first, it actually worked and would randomly spawn the red, green or blue, I don't know if i accidentally changed something but for the last two days it has just been spawning the red one.If someone could help that would be great. Thanks.
Upvotes: 2
Views: 61
Reputation: 6061
You're not actually doing anything with your random ball in spawnBalls. try this
func spawnBalls() {
let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: 0, y: 250)
ball.size = CGSize(width: 70, height: 70)
self.addChild(ball)
}
Upvotes: 1