swift: 检测分组中停止了哪些skaction
swift: detect which skaction stopped in group
我有一个节点熊,上面有两个 skactions(在一个组中)。动作 bearmove 是循环的,而 moveUp 不是,moveUP 应该只发生一次。
let moveUp=SKAction.moveTo(y:50,duration:1.0)
let bearAnimate=[SKTexture(imageNamed:"image1"),SKTexture(imageNamed:"image2"),SKTexture(imageNamed:"image3"),SKTexture(imageNamed:"image2")]
let bearmove=SKAction.animate(with: bearAnimate,timePerFrame:0.3)
let bearRep=SKAction.repeatForever(bearmove)
let bearRun = SKAction.group([moveUp, bearRep])
bear.run(bearRun)
我想知道 moveUp 何时完成/停止 运行。我试过了:
if !bear.hasActions(){
print("stoped moving")
}
但由于 bearAnimate 处于循环状态并且永不停止。这行代码永远不会被读取。任何想法如何检测组中的一个动作?
您可以创建另一个动作来处理移动后发生的事情,然后运行将这些动作作为一个序列。
let afterMoveHandler = SKAction.runBlock({
// Your code here
})
let moveSequence = SKAction.sequence([moveUp, afterMoveHandler])
let bearRun = SKAction.group([moveSequence, bearRep])
如果您想要 SKAction 的扩展来执行您正在寻找的操作,请考虑以下事项:
(代码为 Swift 3)
extension SKAction
{
static func run(action:SKAction,completion:()->()) -> SKAction
{
return SKAction.sequence([action, SKAction.run(completion)]
}
}
用法:
let moveUpEvent = SKAction.run(action:moveUp){/*do something here*/}
let bearRun = SKAction.group([moveUpEvent , bearRep])
我有一个节点熊,上面有两个 skactions(在一个组中)。动作 bearmove 是循环的,而 moveUp 不是,moveUP 应该只发生一次。
let moveUp=SKAction.moveTo(y:50,duration:1.0)
let bearAnimate=[SKTexture(imageNamed:"image1"),SKTexture(imageNamed:"image2"),SKTexture(imageNamed:"image3"),SKTexture(imageNamed:"image2")]
let bearmove=SKAction.animate(with: bearAnimate,timePerFrame:0.3)
let bearRep=SKAction.repeatForever(bearmove)
let bearRun = SKAction.group([moveUp, bearRep])
bear.run(bearRun)
我想知道 moveUp 何时完成/停止 运行。我试过了:
if !bear.hasActions(){
print("stoped moving")
}
但由于 bearAnimate 处于循环状态并且永不停止。这行代码永远不会被读取。任何想法如何检测组中的一个动作?
您可以创建另一个动作来处理移动后发生的事情,然后运行将这些动作作为一个序列。
let afterMoveHandler = SKAction.runBlock({
// Your code here
})
let moveSequence = SKAction.sequence([moveUp, afterMoveHandler])
let bearRun = SKAction.group([moveSequence, bearRep])
如果您想要 SKAction 的扩展来执行您正在寻找的操作,请考虑以下事项:
(代码为 Swift 3)
extension SKAction
{
static func run(action:SKAction,completion:()->()) -> SKAction
{
return SKAction.sequence([action, SKAction.run(completion)]
}
}
用法:
let moveUpEvent = SKAction.run(action:moveUp){/*do something here*/}
let bearRun = SKAction.group([moveUpEvent , bearRep])