我在试着玩漂流游戏。为了使汽车漂移,汽车(转弯时)需要有一个角度。我已经尝试了旋转,但是这与我已经拥有的代码相冲突,我已经转向汽车。这是我的密码,有什么帮助吗?
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)
}}
此代码目前没有增加任何角度,或“漂移”效果的汽车旋转。
发布于 2022-05-31 19:02:56
一种方法是,您可以将您的车嵌套在另一个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而不是汽车的位置
https://stackoverflow.com/questions/72265723
复制相似问题