在SpriteKit中,我想放置一个太空飞船精灵,以便:
(a)顺时针或逆时针旋转。
(b)精灵旋转,并且鼻子总是指向中心,无论它在圆上的什么位置。
当前我有以下输出:
...
我的理想输出如下所示
我的代码如下:
GameScene类:SKScene {
sudo nginx -t
}
我试图添加一个nginx: [warn] conflicting server name "index" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "index.html" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "index.htm" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "index" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "index.html" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "index.htm" on 0.0.0.0:80, ignored
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")
}
但是这里的问题是它自己围绕路径动画,这不是我所追求的。相反,只有用户触摸才能使船顺时针或逆时针移动。
我如何确保:
(a)子画面位于圆形路径上 (b)精灵的鼻子总是指向圆心
非常感谢
答案 0 :(得分:3)
您可以通过将船只添加到容器(SKNode
)中,将船只定位在圆上的一点(零度),将船只指向圆心的方式来实现,旋转容器。这是一个例子...
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())
}