SpriteKit:顺时针旋转船绕一圈
SpriteKit: Rotate a ship clockwise around a circle
在 SpriteKit 中,我想定位一个宇宙飞船精灵,以便:
(a) 顺时针或逆时针旋转。
(b) 精灵旋转,鼻子始终指向中心,无论它在圆上的哪个位置。
目前我有这个输出:
...
我的理想输出如图
我的代码如下:
class 游戏场景:SKScene {
lazy var circleNode:SKShapeNode = {
var circle = SKShapeNode(circleOfRadius: 200)
circle.position = CGPoint(x: frame.midX, y: frame.midY)
circle.strokeColor = SKColor.purple
circle.lineWidth = 10.0
circle.glowWidth = 2.0
circle.physicsBody?.isDynamic = false
return circle
}()
lazy var shipNode: SKSpriteNode = {
var sprite = SKSpriteNode(imageNamed: "ship")
sprite.position = CGPoint(x: circleNode.frame.minX, y: circleNode.frame.minY)
sprite.xScale = 2
sprite.yScale = 2
sprite.zPosition = 1
return sprite
}()
class func newGameScene() -> GameScene {
// Load 'GameScene.sks' as an SKScene.
guard let scene = SKScene(fileNamed: "GameScene") as? GameScene else {
print("Failed to load GameScene.sks")
abort()
}
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
return scene
}
func setUpScene() {
self.addChild(circleNode)
self.addChild(shipNode)
}
override func didMove(to view: SKView) {
self.setUpScene()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
if location.x > shipNode.position.x {
let moveToRight = SKAction.moveBy(x: 1, y: 0, duration: 5)
let forever = SKAction.repeatForever(moveToRight)
shipNode.run(forever, withKey: "move")
}else{
let moveToLeft = SKAction.moveBy(x: -1, y: 0, duration: 5)
let forever = SKAction.repeatForever(moveToLeft)
shipNode.run(forever, withKey: "move")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
shipNode.removeAction(forKey: "move")
}
}
我尝试添加一个SKAction.follow
func setUpScene() {
self.addChild(circleNode)
self.addChild(shipNode)
let circularMove = SKAction.follow(circleNode.path!, asOffset: false, orientToPath: true, duration: 5)
shipNode.run(SKAction.repeat(circularMove,count: 2))
}
但这里的问题是它在路径周围自行设置动画,这不是我想要的。相反,只有用户触摸才能让船顺时针或逆时针移动。
我如何确保:
(a) 精灵坐在圆形路径上
(b) 精灵的鼻子总是指向圆心
非常感谢
您可以通过将您的船添加到一个容器(一个 SKNode
),将船定位在圆上的一个点(0 度),将船指向圆心来实现这一点,然后旋转容器。这是一个例子...
let sprite = SKSpriteNode(imageNamed:"your_ship_name")
let container = SKNode()
override func didMove(to view:SKView) {
let numRotations:CGFloat = 100
let circleRadius:CGFloat = 200
addChild(container)
// Rotate so the ship initially points to the center
sprite.zRotation = CGFloat.pi/2
sprite.position = CGPoint(x: circleRadius, y: 0)
container.addChild(sprite)
let rotate = SKAction.rotate(byAngle: 2 * CGFloat.pi * numRotations, duration: 5 * TimeInterval(numRotations))
container.run(rotate.reversed())
}
在 SpriteKit 中,我想定位一个宇宙飞船精灵,以便:
(a) 顺时针或逆时针旋转。
(b) 精灵旋转,鼻子始终指向中心,无论它在圆上的哪个位置。
目前我有这个输出:
...
我的理想输出如图
我的代码如下:
class 游戏场景:SKScene {
lazy var circleNode:SKShapeNode = {
var circle = SKShapeNode(circleOfRadius: 200)
circle.position = CGPoint(x: frame.midX, y: frame.midY)
circle.strokeColor = SKColor.purple
circle.lineWidth = 10.0
circle.glowWidth = 2.0
circle.physicsBody?.isDynamic = false
return circle
}()
lazy var shipNode: SKSpriteNode = {
var sprite = SKSpriteNode(imageNamed: "ship")
sprite.position = CGPoint(x: circleNode.frame.minX, y: circleNode.frame.minY)
sprite.xScale = 2
sprite.yScale = 2
sprite.zPosition = 1
return sprite
}()
class func newGameScene() -> GameScene {
// Load 'GameScene.sks' as an SKScene.
guard let scene = SKScene(fileNamed: "GameScene") as? GameScene else {
print("Failed to load GameScene.sks")
abort()
}
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
return scene
}
func setUpScene() {
self.addChild(circleNode)
self.addChild(shipNode)
}
override func didMove(to view: SKView) {
self.setUpScene()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
if location.x > shipNode.position.x {
let moveToRight = SKAction.moveBy(x: 1, y: 0, duration: 5)
let forever = SKAction.repeatForever(moveToRight)
shipNode.run(forever, withKey: "move")
}else{
let moveToLeft = SKAction.moveBy(x: -1, y: 0, duration: 5)
let forever = SKAction.repeatForever(moveToLeft)
shipNode.run(forever, withKey: "move")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
shipNode.removeAction(forKey: "move")
}
}
我尝试添加一个SKAction.follow
func setUpScene() {
self.addChild(circleNode)
self.addChild(shipNode)
let circularMove = SKAction.follow(circleNode.path!, asOffset: false, orientToPath: true, duration: 5)
shipNode.run(SKAction.repeat(circularMove,count: 2))
}
但这里的问题是它在路径周围自行设置动画,这不是我想要的。相反,只有用户触摸才能让船顺时针或逆时针移动。
我如何确保:
(a) 精灵坐在圆形路径上 (b) 精灵的鼻子总是指向圆心
非常感谢
您可以通过将您的船添加到一个容器(一个 SKNode
),将船定位在圆上的一个点(0 度),将船指向圆心来实现这一点,然后旋转容器。这是一个例子...
let sprite = SKSpriteNode(imageNamed:"your_ship_name")
let container = SKNode()
override func didMove(to view:SKView) {
let numRotations:CGFloat = 100
let circleRadius:CGFloat = 200
addChild(container)
// Rotate so the ship initially points to the center
sprite.zRotation = CGFloat.pi/2
sprite.position = CGPoint(x: circleRadius, y: 0)
container.addChild(sprite)
let rotate = SKAction.rotate(byAngle: 2 * CGFloat.pi * numRotations, duration: 5 * TimeInterval(numRotations))
container.run(rotate.reversed())
}