2017-05-24 7 views
1

私は現在、spritekitを使って迅速にゲームを進めています。私はthisライブラリを使用して私のジョイスティックを動作させました。swift/spritekit move then touch要素

func updateJoystickPosition(pos: CGPoint) { 
    let posX = -(self.sceneSize.width/2) + pos.x 
    let posY = (self.sceneSize.height/2) - pos.y 
    let newJoystickPos = CGPoint(x: posX, y: posY); 

    self.joystick.position = newJoystickPos 
} 

もうまく働く、しかし、ジョイスティックがその1つのタップに従事していません。私はまた、ユーザーは、このコードを使用して、タップどこにジョイスティックを動かしています。実際のジョイスティックノードをタップしてジョイスティックを操作する必要があります。そのため、ユーザーが指を画面に置いて、すぐに移動してプレイヤーを移動させたいと思っています。とにかく、私は望ましい結果を達成するためにtouchesBegan機能や私の他の機能のいずれかが変更することができますされています:それは自己だ

ジョイスティックは、この機能を

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

    if let touch = touches.first, stick == atPoint(touch.location(in: self)) { 
     tracking = true 
     startHandler?() 
    } 
} 

質問の追跡を開始しますか?

+0

がどのようにあなたの "updateJoystickPosition" 関数が呼び出されるのでしょうか?別のタッチリスナーを追加しましたか?あなたが同じ "touchesBegan"関数を使うだけで、タッチがジョイスティックの外側で起きたときに "tracking = true and startHandler?()"を呼び出すこともできます。 – Pochi

+0

その関数は私のgamesceneの 'touchesBegan'リスナーから呼び出されます。ジョイスティックライブラリは独自のライブラリです。 public functionに 'tracking = true'と' startHandler?() 'を入れ、' self.joystick.position = newJoystickPos'、 'self.analogJoystick.touchJoystick()'のように位置を移動した後に呼び出すようにしましたが、それは不幸にもそれに付いているようだ。 –

答えて

1

touchesMoved(_ touches: ,with event:)を無効にする必要があります。この方法では、すべてtouchesを処理するだけです。例えば

var startTouchingPoint = CGPoint.zero // property 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     startTouchingPoint = touch.location(in: self.camera!)) 
     yourControllerPlace.position = startTouchingPoint 
     stickOnYourJoystick.position = CGPoint.zero 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let point = touch.location(in: self.camera!)) 
     let convertedPoint = CGPoint(x: point.x - startTouchingPoint.x, 
            y: point.y - startTouchingPoint.y) 

     stickOnYourJoystick.position = convertedPoint 

    } 
} 
+0

良い答え:D! – Fluidity