我在屏幕中央有一个精灵,它可以向左或向右旋转,这是通过触摸屏幕的左侧或右侧部分来确定的。
我想要做的是让精灵不断向前移动,但始终朝着它所面对的方向移动。我知道如何使用 SKActions 等进行基本运动……但不知道如何计算在精灵旋转到的方向上连续运动?
数学从来都不是我的强项,所以非常感谢一些示例代码来帮助我。
var player = SKSpriteNode()
override func didMove(to view: SKView) {
player = SKSpriteNode(imageNamed: "4B.png")
player.setScale(0.3)
player.zPosition = 100
self.addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self)
if position.x < 0 {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 2))
player.run(rotate, withKey: "rotating")
} else {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(-M_PI), duration: 2))
player.run(rotate, withKey: "rotating")
}
}
}
override func touchesEnded(_ touches: …Run Code Online (Sandbox Code Playgroud)