2016-08-21 4 views
1

私はジョイスティックで制御されているこのノードを持っています。ジョイスティックを左に動かすと、ジョイスティックを右に動かすとノードのイメージと同じものを変更したいと思います。どうすればいい?ここに私が持っているコードはあります:ジョイスティックを左または右に動かしているときにノードのテクスチャを変更するにはどうすればよいですか?

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch in touches { 
     let location = touch.locationInNode(self) 

     if stickActive == true { 

      //EDIT Code to change texture when moving joystick to left or right. 

      if joyStick.position.x < base.position.x { 

       plane.texture = SKTexture(imageNamed: "planeleft") 

      } else { 

       plane.texture = SKTexture(imageNamed: "planeright") 

      } 



     plane.removeActionForKey("stopaction") 
     plane.physicsBody?.affectedByGravity = false 


     xJoystickDelta = location.x - base.position.x 
     yJoystickDelta = location.y - base.position.y 


     let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.x) 
     let angle = atan2(v.dy, v.dx) 
     let length: CGFloat = base.frame.size.height/2 
     let xDist: CGFloat = sin(angle - 1.57079633) * length 
     let yDist: CGFloat = cos(angle - 1.57079633) * length 


     if (CGRectContainsPoint(base.frame, location)) { 
      joyStick.position = location 
     }else { 

      base.alpha = 0.5 //sets the opacity to 0.5 when the joystick is touched 
      joyStick.position = CGPointMake(base.position.x - xDist, base.position.y + yDist) 

     } 
     } 
    } 
    } 

override func update(currentTime: CFTimeInterval) { 

    let xScale = 0.08 
    let yScale = 0.08 

    let xAdd = self.xJoystickDelta * CGFloat(xScale) 
    let yAdd = self.yJoystickDelta * CGFloat(yScale) 

    self.plane.position.x += xAdd 
    self.plane.position.y += yAdd 
} 

答えて

1

私はこれに慣れていませんが、私はあなたがこれに使うべき論理を知っています。

複雑な:それは真ん中にいたときに、ジョイスティックの中心部に行くことができるジョイスティックが左にある場合、あなたは遠いの範囲を使用する必要があります検出するため

はジョイスティックを左に。右側は中央から最も遠い右に行くことができます。

シンプル:左用

、ジョイスティックはデフォルトの中心位置よりも小さい場合、それX値にします。右は中央よりもXの位置になります。

これは更新機能の中のステートメントであるため、常にこれをチェックしています。

plane.texture = SKTexture(imageNamed: "TextureName") 

注:文ではこのようなものになるだろうこれは、すべての希望、このことができます

をジョイスティックが固定位置にあることを前提とセットセンターがあります!

+0

ありがとう、私はそれを働かせました。私はオペアンプでコードを更新しました! – coding22

関連する問題