【问题标题】:Rotate SKSpritenode by a changing number of degrees将 SKSpritenode 旋转不同的度数
【发布时间】:2017-06-14 04:38:43
【问题描述】:

如何定义 SKAction,然后更新我的节点将旋转的度数?我正在尝试使用变量来定义它,但是当我更新变量值时,操作不会更新。

var degreesToRotate = 4
var direction = 1

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

direction = -1

【问题讨论】:

  • 你应该使用SKAction.rotate(byAngle 与弧度,而不是度数,看看here
  • 它给了我一个“预期的分隔符”错误:let action = SKAction.rotate(byAngle radians: CGFloat(degreesToRotate * direction), duration: TimeInterval(2))
  • @John 这就是你调用该方法的方式:let action = SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1),不要只是从文档中复制和粘贴代码。
  • 如何更新节点应该旋转的角度?
  • @John,试着解释一下你想要实现什么样的轮换?您想要一个在一个方向上无限旋转节点的循环吗?或者您只想将节点旋转一定度数?还是别的什么?

标签: sprite-kit swift3 skspritenode skaction


【解决方案1】:
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

弧度与度数

如您所见,我说的是度数,而不是弧度...那是因为很难说,我将精灵旋转了 6.2831853072 弧度 :) 这就是为什么我使用扩展来进行转换从度数到弧度数,反之亦然。您可能经常使用它,所以我为您添加了它们。

【讨论】:

  • 正是我想要的!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多