当 SKNode 在 运行 一个 SKAction 的中间时,是否可以改变它的位置?

Is it possible to change the position of an SKNode while it is in the middle of running an SKAction?

这是我尝试执行上述操作的尝试,但没有成功:

let square = SKShapeNode(rectOfSize: CGSize(width:60, height: 80))
var isConditionMet = false

override func didMoveToView(view: SKView) {
    square.position.x = 0
    square.position.y = 0
    addChild(square)
    var moveSquare:SKAction
    moveSquare = SKAction.moveTo(CGPoint(x: 100, y: 100), duration: NSTimeInterval(5))
    square.runAction(SKAction.sequence([moveSquare, SKAction.removeFromParent()]))
}

func checkThenChangePosition(shape:SKShapeNode) {
    if isConditionMet == true {
        shape.position.x = size.width
    }
}

override func update(currentTime: CFTimeInterval) {
    print(square.position.x)
    if (square.position.x > 45) && (square.position.x < 50) {
        isConditionMet = true
    }
    checkThenChangePosition(square)
}

使用上面的代码,我希望正方形从 (0,0) 开始并向 (100,100) 移动。一旦方块的位置在 45 和 50 之间(通常在 45.5 左右,因为 SKAction 不会将方块移动整数值),方块应该从其当前 x 位置更改为 size.width 的任何值(在iPhone 6模拟器是375.0).

然而,事实并非如此。相反,在 SKAction 完成之前,方块不会移动到 x = 375.0(一旦方块达到 (100,100))。有什么办法可以让正方形在运行 SKAction中间改变位置,然后继续运行 SKAction。基本上我希望方块从 x = 0 移动到 45 < x < 50,然后传送到 x = 375,然后从 x = 375 移动到 x = 100。提前致谢。

您必须先停止该操作,然后才能应用新操作

    let destination = CGPoint(x: 100, y: 100)
    let moveSquare = SKAction.moveTo(destination, duration: NSTimeInterval(3))

    square.runAction(moveSquare)

    self.runAction(SKAction.sequence([SKAction.waitForDuration(1.0), SKAction.runBlock( {
        self.square.removeAllActions()
        self.square.position = CGPoint(x: 300, y: 300)
        self.square.runAction(moveSquare)
    } )]))

专门针对您的案例

func checkThenChangePosition(shape:SKShapeNode) {
        if isConditionMet == true {
            shape.removeAllActions()
            shape.position.x = self.size.width
            //rerun action to send shape to original location
            //you will have to give this object access to moveSquare either pass it along or make it globally accessible
            //another thing to consider is that the duration may need to be adjusted to keep the speed of the shape consistent
            shape.runAction(moveSquare)
        }
    }