停止 SKAction.applyForce

Stopping SKAction.applyForce

我有一个在屏幕上弹跳的球,调用函数 touchesEnded 时球开始弹跳:

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{

        let location = touch.location(in: self)

        let ballposition = CGVector.init(dx: location.x, dy: location.y - circle.position.y)

        circle.run(SKAction.applyForce(ballposition, duration: 0.3), withKey: "move")


    }}

但是,我有碰撞检测,所以当球击中一个名为 bottom 的节点时,我会收到通知

    if contact.bodyA.node?.name == "Bottom" && contact.bodyB.node?.name == "circle" {
            circle.removeAction(forKey: "move")
            print("Hit")

        }
    }

ball 在代码中命名为 circle

我希望动作 SKAction.applyForce 在碰撞发生时停止 我得到 "Hit" 这个词,这意味着检测工作正常但是 circle.removeAction(forKey: "move") 不起作用,我知道为什么。

我也试过circle.removeAllActions()

这个圆圈与您要添加动作的圆圈是同一个对象吗? 尝试circle.paused = true

SKAction.applyForce 方法仅将力应用于物理 body。移除钥匙将停止施加力,但不会移除当前效果。要停止 body,您可以更改 body 的速度矢量:

 circle.physicsBody?.velocity = CGVector.zero