2

这是我的代码:

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 编程相当陌生,我觉得我忽略了一些小细节,这真的让我很烦。任何帮助表示赞赏!

4

3 回答 3

1

for 循环的执行速度比你想象的要快!您的 for 循环可能会立即执行。

“为什么?我已经打电话self.runAction(delay)延迟for循环了!” 您问。好吧,不幸的是,延迟操作实际上并没有延迟 for 循环。它只会延迟您运行的其他操作self

要解决此问题,请尝试延迟不同的数量,具体取决于 for 循环计数器:

self.runAction(SKAction.waitForDuration(i * 2 + 2)) { ... }

这样,球将每两秒出现一次。

于 2016-10-13T06:11:23.940 回答
1

试试这个,可能是因为你同时为每个球添加动作,

smallBall.runAction(SKAction.sequence([SKAction.waitForDuration(0.1 + (i * 0.35/2)), moveAction, SKAction.runBlock({
                smallBall.removeFromParent()
            })])) // delay added (0.1 + (increment the daly))

尝试这个

于 2016-10-13T05:21:14.757 回答
0

您的问题是您在 for 循环的每次迭代中调用 runAction 。这意味着您的所有操作将同时运行。

您要做的是创建一系列操作。然后在 for 循环之后,运行一系列操作。

(注意:我冒昧地清理了您的代码)

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)
    var sequence = [SKAction]()
    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)
        var ballname = "smallBall”
        switch chanceToSpawn {


        case 61...80:
            //same as case 0…60, smallBall changed to “mediumBall”
            ballname = "mediumBall”

        case 81...99:
            //same as case 0…60, smallBall changed to “largeBall”
            ballname = "largeBall”

        default: ()
        } // ends switch statement

        sequence.append(delay)
        sequence.append(
            SKAction.run()
            {
                [unowned self] in
                let ball = SKSpriteNode(imageNamed: ballName)
                self.addChild(ball)
                ball.xScale = 0.05
                ball.yScale = 0.05
                ball.position = point

                let finalDest = CGPointMake(origin.x, origin.y)
                let moveAction = SKAction.moveTo(finalDest, duration: 5.0)
                let briefWait = SKAction.waitForDuration(0.1)
                let remove = SKAction.removeFromParent()
                let moveSeq = SKAction.sequence([briefWait ,moveAction,remove])
                ball.runAction(moveSeq)

            }
        )
    }//ends for loop


   self.run(SKAction.sequence(sequence))
}//end didMoveToView
于 2016-10-13T13:58:33.973 回答