如何在路径中将精灵移动到中心,然后 return 到原始路径?
How to move sprite to center while is in a path and then return to the original path?
我正在制作一个精灵围绕一个大圆圈旋转的游戏,我希望当触摸开始时精灵移动到屏幕中心的另一个圆圈,现在精灵围绕它旋转,并且当触摸开始时,精灵 return 再次出现到大圆圈。在我的代码中,精灵移动到 -sprite.position.x * 2 以便精灵移动到大圆圈的对面。我该如何解决?
在圆形路径中移动对象的最简单方法之一是
- 创建一个SKNode容器
- 创建精灵
- 将容器添加到场景
- 将精灵添加到容器中
- 将精灵的 x 位置设置为圆形路径的半径
- 旋转容器
如果你想将精灵移动到另一个不同半径的圆,
- 更改精灵的 x 位置
- 移动容器的位置(可选)
如果要改变旋转方向,
- 反转容器的旋转
这是一个例子:
// Define circle
struct Circle {
var position:CGPoint
var radius:CGFloat
}
class GameScene: SKScene {
// 1. Create container node
let node = SKNode()
// 2. Create a sprite
let sprite = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(10,10))
var rotation:CGFloat = CGFloat(M_PI)
var circles:[Circle] = []
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
circles.append(Circle(position: view.center, radius: 80))
circles.append(Circle(position: view.center, radius: 40))
// 3. Add the container to the scene
addChild(node)
// 4. Add the sprite to the container
node.addChild(sprite)
// 5. Set the sprite's x position to the radius of the circular path
if let circle = nextCircle() {
node.position = circle.position
sprite.position = CGPoint(x:circle.radius, y:0)
// 6. Rotate the container
rotate()
}
}
// Rotate the container
func rotate() {
let action = SKAction.rotateByAngle(rotation, duration: 4)
node.runAction(SKAction.repeatActionForever(action),withKey: "rotate")
}
// 9. Reverse the container's rotation
func reverse() {
rotation = -rotation
}
// Stop rotating the container
func stopRotation() {
if node.actionForKey("rotate") != nil {
node.removeActionForKey("rotate")
}
}
// Returns the next circle's parameters and rotates the array
func nextCircle() -> Circle? {
guard let circle = circles.first else {
return nil
}
// Move the current circle to the back of the queue
circles.append(circles.removeAtIndex(0))
return circle
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Change the sprite's x-position */
if sprite.actionForKey("move") == nil {
stopRotation()
// 7. change the sprite's x position
// 8. move the container's position (optional)
if let circle = nextCircle() {
let radius = CGPoint(x:circle.radius, y:0)
let moveCenter = SKAction.moveTo(circle.position, duration: 3)
let move = SKAction.moveTo(radius, duration: 3)
let rotate = SKAction.runBlock {
self.rotate()
}
sprite.runAction(SKAction.sequence([move, rotate]), withKey: "move")
node.runAction(moveCenter, withKey: "move")
}
}
}
}
我正在制作一个精灵围绕一个大圆圈旋转的游戏,我希望当触摸开始时精灵移动到屏幕中心的另一个圆圈,现在精灵围绕它旋转,并且当触摸开始时,精灵 return 再次出现到大圆圈。在我的代码中,精灵移动到 -sprite.position.x * 2 以便精灵移动到大圆圈的对面。我该如何解决?
在圆形路径中移动对象的最简单方法之一是
- 创建一个SKNode容器
- 创建精灵
- 将容器添加到场景
- 将精灵添加到容器中
- 将精灵的 x 位置设置为圆形路径的半径
- 旋转容器
如果你想将精灵移动到另一个不同半径的圆,
- 更改精灵的 x 位置
- 移动容器的位置(可选)
如果要改变旋转方向,
- 反转容器的旋转
这是一个例子:
// Define circle
struct Circle {
var position:CGPoint
var radius:CGFloat
}
class GameScene: SKScene {
// 1. Create container node
let node = SKNode()
// 2. Create a sprite
let sprite = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(10,10))
var rotation:CGFloat = CGFloat(M_PI)
var circles:[Circle] = []
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
circles.append(Circle(position: view.center, radius: 80))
circles.append(Circle(position: view.center, radius: 40))
// 3. Add the container to the scene
addChild(node)
// 4. Add the sprite to the container
node.addChild(sprite)
// 5. Set the sprite's x position to the radius of the circular path
if let circle = nextCircle() {
node.position = circle.position
sprite.position = CGPoint(x:circle.radius, y:0)
// 6. Rotate the container
rotate()
}
}
// Rotate the container
func rotate() {
let action = SKAction.rotateByAngle(rotation, duration: 4)
node.runAction(SKAction.repeatActionForever(action),withKey: "rotate")
}
// 9. Reverse the container's rotation
func reverse() {
rotation = -rotation
}
// Stop rotating the container
func stopRotation() {
if node.actionForKey("rotate") != nil {
node.removeActionForKey("rotate")
}
}
// Returns the next circle's parameters and rotates the array
func nextCircle() -> Circle? {
guard let circle = circles.first else {
return nil
}
// Move the current circle to the back of the queue
circles.append(circles.removeAtIndex(0))
return circle
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Change the sprite's x-position */
if sprite.actionForKey("move") == nil {
stopRotation()
// 7. change the sprite's x position
// 8. move the container's position (optional)
if let circle = nextCircle() {
let radius = CGPoint(x:circle.radius, y:0)
let moveCenter = SKAction.moveTo(circle.position, duration: 3)
let move = SKAction.moveTo(radius, duration: 3)
let rotate = SKAction.runBlock {
self.rotate()
}
sprite.runAction(SKAction.sequence([move, rotate]), withKey: "move")
node.runAction(moveCenter, withKey: "move")
}
}
}
}