通过用户滑动将脉冲应用到 SKSpriteNode
Apply impulse to a SKSpriteNode from a user swipe
谁能告诉我为什么当我尝试向这样的节点施加脉冲时它会消失?
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 方向获取值的另一种方法。如果我尝试将方向本身用作脉冲矢量,它就会崩溃。有什么见解吗?谢谢!
当我从 direction.dx 和 direction.dy 中删除 .hashValue 时,您的代码对我有用(散列值用于 comparisons)。哦,你的球节点并没有消失。这些哈希值足够大,以至于当您将它们作为力施加到球上时,球的速度足够大,以至于在渲染下一帧时球已经离开屏幕。
谁能告诉我为什么当我尝试向这样的节点施加脉冲时它会消失?
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 方向获取值的另一种方法。如果我尝试将方向本身用作脉冲矢量,它就会崩溃。有什么见解吗?谢谢!
当我从 direction.dx 和 direction.dy 中删除 .hashValue 时,您的代码对我有用(散列值用于 comparisons)。哦,你的球节点并没有消失。这些哈希值足够大,以至于当您将它们作为力施加到球上时,球的速度足够大,以至于在渲染下一帧时球已经离开屏幕。