duration内时,不遵循moveTorunBlock属性,允许序列中的后续操作仅在duration秒后执行,因此立即执行。

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

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(序列未正确执行):
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()
}]))

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

Code B中,itemBbadMoveAction完成之前会被删除,这意味着itemB绝不会从其原始位置移动。好像Code B中不使用duration属性。

我们如何像itemB中一样移动Code B,但是确保badMoveAction完成之前序列中的下一个 Action 才开始?

最佳答案

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

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()
        })

    })
]))

08-16 14:51