这是我的代码:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let origin = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
let delay = SKAction.waitForDuration(2.0)
for (var i = 0; i < 6; i++) {
//generate a random point on a circle centered at origin with radius of 500
let point = randomPointOnCircle(500, center: origin)
let chanceToSpawn = arc4random_uniform(100)
switch chanceToSpawn {
case 0...60:
self.runAction(delay){
let smallBall = SKSpriteNode(imageNamed: "smallBall”)
self.addChild(smallBall)
smallBall.xScale = 0.05
smallBall.yScale = 0.05
smallBall.position = point
let finalDest = CGPointMake(origin.x, origin.y)
let moveAction = SKAction.moveTo(finalDest, duration: 5.0)
smallBall.runAction(SKAction.sequence([SKAction.waitForDuration(0.1), moveAction, SKAction.runBlock({
smallBall.removeFromParent()
})]))
delay
}
break
case 61...80:
//same as case 0…60, smallBall changed to “mediumBall”
break
case 81...99:
//same as case 0…60, smallBall changed to “largeBall”
break
default:
break
} // ends switch statement
}//ends for loop
}//end didMoveToView
基本上,我的球会在圆圈上的随机点上产生并朝中心移动。但是我遇到的问题是,无论我如何重新排列事物(或重写事物,即尝试不同的循环或定义用于生成球和球运动的函数,并简单地从 switch 语句中调用它们),所有球同时产生。我试图让它一个接一个地产生(意味着一个球产生,线程等待几秒钟,然后产生另一个球。),我似乎无法弄清楚为什么每个球同时产生时间和所有其他球一样。
我对 Swift/Spritekit 编程相当陌生,我觉得我忽略了一些小细节,这真的让我很烦。任何帮助表示赞赏!