2016-10-28 7 views
0

このようなノードにインパルスを適用しようとすると、なぜ誰に教えてもらえませんか?ユーザスワイプからSKSpriteNodeにインパルスを適用する

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

direction.dxおよびdirection.dyから.hashValueを削除したときのコードは私のために働いていました(ハッシュ値はcomparisonsに使用されています)。ああ、あなたのボールノードは消えていません。これらのハッシュ値は、ボールに力として適用すると、ボールの速度が十分に大きく、次のフレームがレンダリングされるまでにボールが画面外に出るほど大きくなります。

+0

はい、私はhashValueを誤って使用していました。私はこれに加えてスプライトがより現実的に動くように編集しました。ベクトルを縮小する必要があります。 ?(:impulseDirectionX、DY:impulseDirectionY)CGVector(DX) self.L1Ball.physicsBody impulseDirectionX =(direction.dx/4) せimpulseDirectionY =(-direction.dy/4) self.L1Ball.physicsBody .applyImpulseを聞かせ?.affectedByGravity = true – Lyres

+0

それは素晴らしいです。あなたの質問に正しく答えましたか? – Ralph

+0

本当にありがとう! – Lyres

関連する問題