来自spritenode的spritekit反弹效果由手指

时间:2017-02-02 21:49:35

标签: ios swift sprite-kit skspritenode skphysicsbody

我有一个用于在SpriteKit中测试物理的演示应用程序。用平底锅手势投掷精灵后,我能够模拟惯性。当我释放手指弹跳工作很棒。 问题是:如何在拖动过程中模拟弹跳?我想在我拖动SKSpriteNode时将红色SKSpriteNode弹回。

我尝试设置self.selectedNode?.physicsBody?.isDynamic = true,但是即使我设置了正确的位置,精灵也会在手指下移开。

这是我的repo。随意复制或试验。

关键代码:

func handlePan(panGestureRecognizer recognizer:UIPanGestureRecognizer) {
    let touchLocationView = recognizer.location(in: recognizer.view)
    let touchLocationScene = self.convertPoint(fromView: touchLocationView)

    switch recognizer.state {
    case .began:
        self.showMoved = false
        let canditateNode = self.touchedNode(touchLocationScene)
        if let name = canditateNode.name, name.contains(kMovableNode) {
            self.selectedNode = canditateNode
            self.selectedNode?.physicsBody?.isDynamic = false;
        }
    case .changed:
        let translation = recognizer.translation(in: recognizer.view)
        if let position = self.selectedNode?.position {
            self.selectedNode?.position = CGPoint(x: position.x + translation.x, y: position.y - translation.y)
            recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
        }
    case .ended:
        self.selectedNode?.physicsBody?.isDynamic = true;
        let velocity = recognizer.velocity(in: recognizer.view)
        self.selectedNode?.physicsBody?.applyImpulse(CGVector(dx: velocity.x, dy: -velocity.y))
        self.selectedNode = nil
        self.showMoved = true
    default:
        break
    }
}

Can't bounce while dragging, but can bounce after dragging.

0 个答案:

没有答案