通过用户滑动将脉冲应用于SKSpriteNode

时间:2016-10-28 15:21:29

标签: sprite-kit swift3 skspritenode uitouch uievent

有人能告诉我为什么当我尝试对这样的节点施加冲动时它会消失吗?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first! as UITouch
    startPoint = touch.location(in: view)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    defer {
        startPoint = nil
    }
    guard touches.count == 1, let startPoint = startPoint else {
        return
    }
    if let touch = touches.first {
        let endPoint = touch.location(in: view)
        //Calculate your vector from the delta and what not here
        direction = CGVector(dx: endPoint.x - startPoint.x, dy: endPoint.y - startPoint.y)

        L1Ball.physicsBody?.applyImpulse(CGVector(dx: direction.dx.hashValue, dy: direction.dy.hashValue))
        }
}

我认为这与我使用hashValue的方式有关。我不知道从CGVector方向获取值的另一种方法。如果我尝试使用方向本身作为脉冲向量,它就会崩溃。任何见解?谢谢!

1 个答案:

答案 0 :(得分:1)

当我从direction.dx和direction.dy中移除.hashValue时,您的代码对我有用(哈希值用于comparisons)。哦,你的球节点并没有消失。这些哈希值足够大,当你将它们作为力施加在球上时,球的速度足够大,以至于在下一帧渲染时球离开屏幕。