2017-07-31 1 views
0

私の試合では現在、投げられるボールがあります。しかし、ユーザーが特定のポイントを超えてボールを投げることができるようにしたい。私はこれをしばらく試してきましたが、私はそれを理解することはできません。これどうやってするの?画面の特定の領域でスワイプボールを無効にするにはどうすればよいですか? (Swift)(SpriteKit)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    let touch = touches.first as! UITouch? 
    let location = touch?.location(in: self) 
    if ball.frame.contains(location!) { 
     touchPoint = location! 
     touching = true 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    let touch = touches.first as! UITouch? 
    let location = touch?.location(in: self) 
    touchPoint = location! 
} 
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     touching = false 
} 

override func update(_ currentTime: TimeInterval) { 

    if touching { 
     let dt:CGFloat = 2.8/60.0 
     let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y) 
     let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt) 
     ball.physicsBody!.velocity=velocity 
    } 
} 

ここで私は今までの自分のゲームのイメージを持っています。現在、ボールは画面上のどこにでもドラッグ、スロー、スワイプすることができます。しかし、私はボールをタッチ、スロー、または画面の中央の下にドラッグすることができますようにしたい。しかし、ユーザーの指が誤って限界に近づくとボールが投げられ続けるようにしたい。

enter image description here

+0

さらにいくつかの説明や図(さえ描かれた)でありますあなたはあなたが達成しようとしている道に沿って行くだろう。または、これまでに試したことのいくつかの例を表示してください –

+0

画像を追加しました:) @RonMyschuk –

+0

"しかし、私の指が誤って限界に近づくとボールが投げられ続けます。 - ジェスチャーが底部の領域で開始しなければならないという考えがここにありますが、一度開始すると目に見えない線の上でそれを追跡し続けますか? –

答えて

0

あなたは、実際のサイズとゲームシーンanchorPointに基づいて、底面積の座標を調整する必要があります。例えば、試料中の「100」は、あなたのシーンは、(0,0)のanchorPointを持っていることを前提と下スクロール領域の大きさは、何の100

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    if let touch = touches.first as UITouch! { 

     let location = touch.location(in: self) 

     guard location < 100 else { return } 

     //if ball.frame.contains(location!) { 
      touchPoint = location! 
      touching = true 
     //} 
    } 
} 
+0

問題が1つあります。私のシーンにはSKCameraがあります。それはまだ動作しますか? –

+0

なぜそれがうまくいかないのか分かりません。必要ならばいつでもカメラのタッチ位置を確認することができます –

関連する問題