2015-12-12 126 views
1

我正在做一個精靈軌道上的點,併爲我做的,後面是精靈的路徑:遵循的路徑逆

let dx1 = base1!.position.x - self.frame.width/2 
    let dy1 = base1!.position.y - self.frame.height/2 

    let rad1 = atan2(dy1, dx1) 

    path1 = UIBezierPath(arcCenter: circle!.position, radius: (circle?.position.y)! - 191.39840698242188, startAngle: rad1, endAngle: rad1 + CGFloat(M_PI * 4), clockwise: true) 
    let follow1 = SKAction.followPath(path1.CGPath, asOffset: false, orientToPath: true, speed: 200) 
    base1?.runAction(SKAction.repeatActionForever(follow1)) 

這一工程和精靈開始軌道圍繞該點。問題是,當用戶觸摸屏幕時,我希望精靈開始逆時針旋轉。對於我寫了相同的代碼的最後一行順時針旋轉,但編輯於:

base1?.runAction(SKAction.repeatActionForever(follow1).reversedAction()) 

但問題是,雖然它逆時針旋轉,精靈圖像翻轉水平。我能做些什麼來避免這種情況?或者還有什麼其他方式通過一個點來繞過精靈?

回答

1

一個簡單的方法來旋轉一個點(即軌道)的精靈是創建一個容器節點,在節點上以偏移量(相對於容器的中心)添加一個精靈,並旋轉容器節點。這裏有一個如何做到這一點的例子:

class GameScene: SKScene { 
    let sprite = SKSpriteNode(imageNamed:"Spaceship") 
    let node = SKNode() 

    override func didMove(to view: SKView) { 
     sprite.xScale = 0.125 
     sprite.yScale = 0.125 
     sprite.position = CGPoint (x:100, y:0) 

     node.addChild(sprite) 
     addChild(node) 

     let action = SKAction.rotate(byAngle:CGFloat.pi, duration:5) 

     node.run(SKAction.repeatForever(action), withKey:"orbit") 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if let action = node.action(forKey: "orbit") { 
      // Reverse the rotation direction 
      node.removeAction(forKey:"orbit") 
      node.run(SKAction.repeatForever(action.reversed()),withKey:"orbit") 
      // Flip the sprite vertically 
      sprite.yScale = -sprite.yScale 
     } 
    } 
}