【问题标题】:Is it possible to change the position of an SKNode while it is in the middle of running an SKAction?是否可以在运行 SKAction 的过程中更改 SKNode 的位置?
【发布时间】:2016-06-19 15:42:19
【问题描述】:

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

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

【问题讨论】:

    标签: swift sprite-kit skaction


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2011-05-04
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 2014-07-14
      相关资源
      最近更新 更多