2015-09-24 38 views
0

hello:D我有下面这段代码。用快捷键在游戏杆中移动精灵Xcode

import SpriteKit 

class GameScene: SKScene { 

    let base = SKSpriteNode(imageNamed: "yellowArt/Base") 
    let ball = SKSpriteNode(imageNamed: "yellowArt/Ball") 
    let ship = SKSpriteNode(imageNamed: "yellowArt/Ship") 

    var stickActive:Bool = false 


    override func didMoveToView(view: SKView) { 





     self.backgroundColor = SKColor.blackColor() 
     self.anchorPoint = CGPointMake(0.5, 0.5) 

     self.addChild(base) 
     base.position = CGPointMake(0, -200) 

     self.addChild(ball) 
     ball.position = base.position 

     self.addChild(ship) 
     ship.position = CGPointMake(0, 200) 

     ball.alpha = 0.4 
     base.alpha = 0.4 
    } 




    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 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)) { 

      ball.position = location 

     } else { 

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

     ship.zRotation = angle - 1.57079633 
     ship.position = CGPointMake(angle, angle) 



     } 
     } // ends stick active test 
    } 
    } 


    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) 
    } 
    } 


    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
    } 
} 

上面的代码创建了一个操纵杆和一个船。通过移动操纵杆,我可以用操纵杆来旋转“船”。但是,我想按照操纵杆的方向移动船。我如何解决这个问题?谢谢。

回答

3

你的船不会去任何地方,因为你有ship.position = CGPointMake(angle, angle).只要你移动操纵杆,船会去那一点 - 删除那条线。

我会用update方法移动船。首先你需要找出你想在x和y方向上移动它多少。

var xJoystickDelta = CGFloat() var yJoystickDelta = CGFloat()

将下面的代码在你的touchesMoved方法:

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

将下面的代码在你update方法:

下方您 var stickActive:Bool = false声明操纵杆移动创建类变量
let xScale = 1.0 //adjust to your preference 
let yScale = 1.0 //adjust to your preference 

let xAdd = xScale * self.xJoytickDelta 
let yAdd = yScale * self.yJoystickDelta 

self.ship.position.x += xAdd 
self.ship.position.y += yAdd 

我希望这有助于。

+0

对于那些未来的读者,如果你想在'touches ended'方法中使用'xJoystickDelta = 0'和'yJoystickDelta = 0'来让你的角色或者你正在移动的任何东西停止移动操纵杆 –