swift spritekit:如何停止运行块?
swift spritekit : How to stop a 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) 只是呈现一个发送密钥 "stop" 的按钮,但块继续 运行 并打印。可以停止这个块吗?
noObject.removeActionForKey("stop")
So you are naming an object "noObject" and an action "stop"? :)
无论如何,您应该使用 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")
如何停止来自 运行 区块的所有操作:
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) 只是呈现一个发送密钥 "stop" 的按钮,但块继续 运行 并打印。可以停止这个块吗?
noObject.removeActionForKey("stop")
So you are naming an object "noObject" and an action "stop"? :)
无论如何,您应该使用 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")