화면의 왼쪽 또는 오른쪽 부분을 터치하여 좌우로 회전 할 수있는 스프라이트가 화면 중앙에 있습니다. .(Xcode Swift SpriteKit) 스프라이트를 향한 방향으로 이동하는 방법
스프라이트가 앞으로 나아갈 때 항상 앞으로 나아갈 방향으로 향하고 있지만 앞으로 향하는 방향으로 향하고 싶습니다. 나는 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: Set<UITouch>, with event: UIEvent?) {
player.removeAction(forKey: "rotating")
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
완벽합니다 - 대단히 감사합니다. –