如何在runBlock完成之前按顺序延迟下一个动作? (迅速) [英] How to delay next action in sequence until after runBlock completes? (Swift)

查看:113
本文介绍了如何在runBlock完成之前按顺序延迟下一个动作? (迅速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持续时间属性在 runBlock内部时未被跟踪,允许序列中的后续操作立即执行,只应在持续时间秒后执行。

The duration property for moveTo isn't followed when inside a runBlock, allowing the subsequent action in a sequence to get executed immediately when it should only get executed after duration seconds.

代码A(序列正确执行):

Code A (sequence properly executed):

let realDest = CGPointMake(itemA.position.x, itemA.position.y)
let moveAction = SKAction.moveTo(realDest, duration: 2.0)

itemB.runAction(SKAction.sequence([SKAction.waitForDuration(0.5), moveAction, SKAction.runBlock {
    itemB.removeFromParent()
}]))

代码B(序列没有正确执行):

Code B (sequence not properly executed):

let badMoveAction = SKAction.runBlock {
    let realDest = CGPointMake(itemA.position.x, itemA.position.y)
    let moveAction = SKAction.moveTo(realDest, duration: 2.0)
    itemB.runAction(moveAction)
}

itemB.runAction(SKAction.sequence([SKAction.waitForDuration(0.5), badMoveAction, SKAction.runBlock {
    itemB.removeFromParent()
}]))

代码A 中, itemB moveAction后被删除完成(约2秒)。这是正确的顺序。

In Code A, itemB gets removed after the moveAction completes (about 2 seconds). This is the correct sequence.

代码B itemB badMoveAction 结束之前删除,意味着 itemB 永远不会从其原始位置移动。好像持续时间属性不符合代码B

In Code B, itemB gets removed before badMoveAction finishes, meaning itemB never moves from its original position. It's as if the duration property isn't honored in Code B.

我们如何移动 itemB ,如代码B 但确保序列中的下一个操作直到 badMoveAction 完成?

How can we move itemB as in Code B but ensure the next action in the sequence doesn't start until badMoveAction completes?

推荐答案

runBlock of SKAction 创建零持续时间的动作,并立即发生。因此,在代码B 中,它将开始执行第一个操作并立即返回以按顺序开始下一个操作。

runBlock of SKAction creates action with zero duration and it takes place instantaneously. So in Code B it will start executing first action and return immediately to start next action in sequence.

如果你想要创建自己的行动,持续时间可以使用 customActionWithDuration:actionBlock: 。否则这里代码A 完全没问题(如果你只想使用移动动作)。

If you want to create your own action with duration you can use customActionWithDuration:actionBlock:. Otherwise here Code A is perfectly fine(if you want to use move action only).

你也可以使用 SKAction.removeFromParent() 删除 itemB 而不是:

Also you can use SKAction.removeFromParent() to remove itemB instead of:

SKAction.runBlock {
itemB.removeFromParent()
}

其他解决方案:

As runBlock 立即执行您可以在删除节点之前添加 waitForDuration 操作。(在runBlock之后添加持续时间等于runBlock)

As runBlock executes immediately you can add waitForDuration action before removing node.(add after runBlock with duration equal to runBlock)

itemB.runAction(SKAction.sequence([SKAction.waitForDuration(0.5),
badMoveAction, SKAction.waitForDuration(2.0), SKAction.removeFromParent()]))

但这可以通过 customActionWithDuration:actionBlock:

But this can be achieved using customActionWithDuration:actionBlock:.

如果你想同时发生两个动作(移动,移除节点),那么你也可以使用在SKAction中。

If you want both action(move, remove node) should occur simultaneously then you can also use group in SKAction.

这篇关于如何在runBlock完成之前按顺序延迟下一个动作? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆