本文介绍了runBlock发生后,在SKAction.sequence中延迟下一个动作(Swift)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持续时间属性在 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?

推荐答案

这应该做你想要的。我只是重新安排了一些代码。

This should do what you want. i just re-arranged the code a little bit.

itemB.runAction(SKAction.sequence([

    // wait for half a second
    SKAction.waitForDuration(0.5),
    SKAction.runBlock({

        // after waiting half a second, get itemA's position
        let realDest = CGPointMake(itemA.position.x, itemA.position.y)
        let moveAction = SKAction.moveTo(realDest, duration: 2.0)

        // move to that position, after we get there, remove itemB from scene
        itemB.runAction(moveAction, completion: {
            itemB.removeFromParent()
        })

    })
]))

这篇关于runBlock发生后,在SKAction.sequence中延迟下一个动作(Swift)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:31