2016-06-29 13 views
1

ジョイスティックの動きに応じて、ジョイスティックを使ってプレーヤーをすべてのダイレクトにどのように移動させることができますか?私は既にジョイスティックを作っており、すでに度付きでセットアップされています。しかし、私はどのようにすべての方向にプレーヤーを移動する?ありがとう。ジョイスティックでスプライトを移動するには?

クラスGameScene:SKScene {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo") 
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo") 
let base = SKSpriteNode(imageNamed: "circuloFondo") 
let ball = SKSpriteNode(imageNamed: "circulo") 
var stickActive:Bool = false 



override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 

    base.size = CGSize(width: 100, height: 100) 
    base.alpha = 0.3 
    base.zPosition = 2.0 
    base.position = CGPoint(x: frame.width/2, y: frame.height/2 - 310) 
    self.addChild(base) 

    ball.size = CGSize(width: 50, height: 50) 
    ball.color = circuloPrincipal.color 
    //ball.alpha = 0 
    ball.zPosition = 3.0 
    ball.position = base.position 
    self.addChild(ball) 

    circuloPrincipal.size = CGSize(width: 35, height: 35) 
    circuloPrincipal.position = CGPoint(x: frame.width/2, y: frame.height/2) 
    self.addChild(circuloPrincipal) 
    circuloPrincipal.color = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1.0) 
    circuloPrincipal.colorBlendFactor = 1.0 
    circuloPrincipal.zPosition = 3.0 
} 

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


    for touch in touches { 
     let location = touch.locationInNode(self) 

     if (CGRectContainsPoint(ball.frame, location)) { 

      stickActive = true 
     }else { 

      stickActive = false 

     } 

    } 
} 

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

     if (stickActive == true) { 


     let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y) 
     let angle = atan2(v.dy, v.dx) 
     let deg = angle * CGFloat(180/M_PI) 
     print(deg + 180) 

     let lenght:CGFloat = base.frame.size.height/2 - 20 
     let xDist: CGFloat = sin(angle - 1.57079633) * lenght 
     let yDist: CGFloat = cos(angle - 1.57079633) * lenght 

     ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist) 

     if (CGRectContainsPoint(base.frame, location)) { 

      ball.position = location 
     }else { 

      ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist) 

     } 


     } // termina stickActive 

    } 

} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if (stickActive == true) { 

     let move: SKAction = SKAction.moveTo(base.position, duration: 0.2) 
     move.timingMode = .EaseOut 

     ball.runAction(move) 

    } 
} 

答えて

1

これは要旨

// this is position of your joystick knob 
let joyStickPos = CGPointMake(20, 30) 
let length = sqrt(joyStickPos.x * joyStickPos.x + joyStickPos.y*joyStickPos.y) 
let normalizedPoint = CGPointMake(joyStickPos.x/length, joyStickPos.y/length) 


// in update method 
let speed = CGFloat(10) 
sprite.position.x += (normalizedPoint.x * speed) 
sprite.position.y += (normalizedPoint.y * speed) 
+0

はなぜプレイヤーは唯一の右にムーバーないのですか? –

+0

ジョイスティックに何か問題がある可能性があります – hamobi

+0

何ができますか? –

関連する問題