2017-01-28 57 views
2

如何定義SKAction,然後更新我的節點將旋轉的度數?我試圖用變量來定義它,但是當我更新變量值時,動作不會更新。將SKSpritenode旋轉不同的度數

var degreesToRotate = 4 
var direction = 1 

let action = SKAction.rotate(byAngle: CGFloat(degreesToRotate * direction), duration: TimeInterval(2)) 
      charector.run(SKAction.repeatForever(action)) 

direction = -1 
+0

你應該使用'SKAction.rotate(byAngle'用弧度,而不是度數,看看[這裏](https://developer.apple.com/reference/spritekit/skaction/1417805-rotate) –

+0

它給了我一個「預期分隔符」的錯誤:let action = SKAction.rotate(byAngle radians:CGFloat(degreesToRotate * direction),duration:TimeInterval(2)) – John

+0

@John這是你如何調用該方法:'let action = SKAction.rotate byAngle:CGFloat(M_PI),持續時間:1)',不要複製和粘貼文檔中的代碼 – Whirlwind

回答

2
import SpriteKit 
import GameplayKit 

//Extensions borrowed from here : http://stackoverflow.com/a/29179878/3402095 

extension Int { 
    var degreesToRadians: Double { return Double(self) * .pi/180 } 
    var radiansToDegrees: Double { return Double(self) * 180/.pi } 
} 
extension FloatingPoint { 
    var degreesToRadians: Self { return self * .pi/180 } 
    var radiansToDegrees: Self { return self * 180/.pi } 
} 

let kActionKey = "rotate" 

class GameScene:SKScene { 

    let purpleCube = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150)) 
    let yellowCube = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150)) 

    override func didMove(to view: SKView) { 


     addChild(purpleCube) 
     purpleCube.position.y = purpleCube.size.height 
     purpleCube.name = "purple" 


     addChild(yellowCube) 
     yellowCube.position.y = -yellowCube.size.height 
     yellowCube.name = "yellow" 

     let rotate = SKAction.rotate(byAngle: CGFloat(-M_PI * 2.0), duration: 5) 
     let loop = SKAction.repeatForever(rotate) 
     purpleCube.run(loop, withKey: kActionKey) 
    } 

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

     super.touchesBegan(touches, with: event) 


     if let touch = touches.first { 

      let location = touch.location(in: self) 

      if let cube = atPoint(location) as? SKSpriteNode { 

       if let name = cube.name { 

        switch name { 

        case "purple": 

         if let action = purpleCube.action(forKey: kActionKey){ 

          purpleCube.run(action.reversed(), withKey: kActionKey) 
         } 


        case "yellow": 

         if action(forKey: "rotating") == nil{ 
          yellowCube.run(SKAction.rotate(byAngle: CGFloat(4.degreesToRadians), duration: 0.1), withKey: kActionKey) 
         } 



        default: 
         break 
        } 
       } 

      } 
     } 

    } 
} 

在這個例子中,存在已經在兩種不同的方式被旋轉的兩個節點。紫色節點以一定的速度順時針旋轉。爲了達到這個目的,我創建了一個將精靈旋轉360度的動作......這將是一次革命,將會永遠重複,因此精靈將永遠旋轉。

關於黃色節點...每次點擊時都會旋轉4度。目前,您必須等待精靈停止旋轉,以便您可以更多地旋轉它。這當然是可選的,我只是想告訴你操作鍵的用處。

旋轉方向

由於SpriteKit 0度指定x軸正和正角度在反時針方向,我通過-360度,其旋轉在順時針方向旋轉時的精靈紫色立方體。要了解有關SpriteKit座標系的更多信息,請閱讀this documentation section

弧度Vs的學位

正如你所看到的,我在講度,而不是弧度......那是因爲它是真的很難說,我通過6.2831853072弧度旋轉精靈: )這就是爲什麼我使用從度數轉換爲弧度的擴展,反之亦然。你可能經常使用它,所以我爲你添加了它們。

+0

正是我在找的東西!非常感謝! – John