swift spritekit:如何停止runBlock?

时间:2016-08-02 00:33:21

标签: swift sprite-kit removeall

如何停止runBlock中的所有操作:

func slideShowControl () {


    let noObject = SKSpriteNode()
    noObject.name = "noObject"

    addChild(noObject)



    let block = SKAction.runBlock({

        self.slideShow(1)
        print("slide01")

        self.runAction(SKAction.waitForDuration(5), completion: {

            print("slide02")


        })

        self.runAction(SKAction.waitForDuration(8), completion: {

            print("slide03")

        })

        ...

        self.runAction(SKAction.waitForDuration(17), completion: {

            print("slide06")

        })

    })


    noObject.runAction(block, withKey: "stop")

self.slideShow(1)只显示一个按钮,发送键“停止”,但该块继续运行并打印。可以阻止这个阻止吗?

noObject.removeActionForKey("stop")

1 个答案:

答案 0 :(得分:1)

  

所以你要命名一个对象" noObject"并采取行动"停止"? :)

无论如何,您应该使用group和一些sequence操作。

想法

此代码

self.runAction(SKAction.waitForDuration(5), completion: {
    print("slide02")
})

也可以这样写出

let action1 = SKAction.sequence([SKAction.waitForDuration(5), SKAction.runBlock { print("slide02") }])

解决方案

给出一个精灵

let sprite = SKSpriteNode()

和4个动作

let action0 = SKAction.runBlock { print("slide01") }
let action1 = SKAction.sequence([SKAction.waitForDuration(5), SKAction.runBlock { print("slide02") }])
let action2 = SKAction.sequence([SKAction.waitForDuration(8), SKAction.runBlock { print("slide03") }])
let action3 = SKAction.sequence([SKAction.waitForDuration(6), SKAction.runBlock { print("slide06") }])

我们可以将这样的行为分组

let group = SKAction.group([action0, action1, action2, action3])

现在我们可以使用键

运行操作
sprite.runAction(group, withKey: "group")

然后停止

sprite.removeActionForKey("group")