2017-08-31 182 views
2
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) 
    } 
} 

} 

每次我的應用程序加載紅球產卵,但它應該隨機產卵紅色,藍色或綠色的球。起初,它實際上是有效的,會隨機產生紅色,綠色或藍色,我不知道是否意外地改變了一些東西,但在過去兩天裏它已經產生了紅色。如果有人能夠幫助,這將是偉大的。謝謝。隨機產卵節點

回答

1

你實際上並沒有用你的隨機球在spawnBalls中做任何事情。試試這個

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) 
}