如何停止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")
答案 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")