在SpriteKit中,我想定位一艘太空船sprite以便:
(a) 顺时针或逆时针旋转。
(b) 精灵旋转,它的鼻子总是指向中心,不管它在圆上的什么位置。
...
我的理想输出如下所示
类游戏场景: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))
}
我如何确保:
(b) 精灵的鼻子总是指向圆心
非常感谢