在旋转期间改变 SKSpriteNode 的角度

Changing the angle of a SKSpriteNode during a rotation

我正在尝试做一款漂移游戏。为了使汽车漂移,汽车(转弯时)需要倾斜。我试过旋转,但这与我已经拥有的用于转动汽车的代码冲突。这是我的代码,有什么帮助吗?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first {
                let location = touch.previousLocation(in: self)
                let position = touch.location(in: self)
                let node = self.nodes(at: location).first

if position.x < 0 {
                    let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 0.8))
                    car.run(rotate, withKey: "rotating")
         
            } else {
                    let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(-M_PI), duration: 0.8))
                    car.run(rotate, withKey: "rotating")
                
               
                
                }
            
        }
            
    }





    override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    car.position = CGPoint(x:car.position.x + cos(car.zRotation) * 3.0,y:car.position.y + sin(car.zRotation) * 3.0)

    }

}

此代码目前在不添加任何角度或“漂移”效果的情况下转动汽车。

一种方法是您可以将您的汽车嵌套在另一个 SKNode 中作为容器。将您的转向旋转应用于汽车节点,就像您现在所做的那样。然后将漂移旋转应用于容器节点。结果效果将是两者的总和。

//embed car inside a SKNode container so you can apply different rotations to each
let car = SKShapeNode(ellipseOf: CGSize(width: 20, height: 40)) //change to how you draw your car
let drift_container = SKNode()
drift_container.addChild(car) //embed car inside the container
self.addChild(drift_container) //`self` here is a SKScene

//apply angle rotation to the container
func drift(byAngle angle:CGFloat) {
    let rotate = SKAction.rotate(toAngle: angle, duration: 0.2)
    drift_container.run(rotate)
}

然后在update一定要更新drift_container.position而不是汽车的位置