【发布时间】:2017-05-08 10:14:06
【问题描述】:
如何快速从CGPoints 数组中为SKSpriteNode 设置动画?我还希望SKSpriteNode 旋转到下一个位置。任何帮助将不胜感激。
【问题讨论】:
标签: ios swift3 sprite-kit
如何快速从CGPoints 数组中为SKSpriteNode 设置动画?我还希望SKSpriteNode 旋转到下一个位置。任何帮助将不胜感激。
【问题讨论】:
标签: ios swift3 sprite-kit
根据@KnightOfDragon 的建议,您可以制作一条路径并让节点跟随它,如下所示:
class GameScene: SKScene {
override func didMove(to view: SKView) {
//1. create points
let points = [
CGPoint(x:frame.minX,y:frame.minY),
CGPoint(x:frame.maxX,y:frame.maxY),
CGPoint(x:frame.maxX,y:frame.midY),
CGPoint.zero
]
//2. Create a path
let path = CGMutablePath()
//3. Define starting point
path.move(to: points[0])
//4. Add additional points
for point in points[1..<points.count]{
print("point : \(point)")
path.addLine(to: point)
}
//5. Create an action which will make the node to follow the path
let action = SKAction.follow(path, speed: 122)
let sprite = SKSpriteNode(color: .white, size: CGSize(width: 100, height: 100))
addChild(sprite)
sprite.run(action, withKey: "aKey")
}
}
如果您希望节点定向到它所遵循的路径,这可能比接受的答案更方便(zRotation 属性动画以便节点转向遵循路径)。
【讨论】:
你可以这样做:
import SpriteKit
class GameScene: SKScene,SKSceneDelegate {
override func didMove(to view: SKView) {
//1. create points
let points = [CGPoint(x:120,y:20),CGPoint(x:220,y:20),CGPoint(x:40,y:320)]
var actions = [SKAction]()
//2. Create actions
for point in points {
actions.append(SKAction.move(to: point, duration: 1))
}
let sprite = SKSpriteNode(color: .white, size: CGSize(width: 100, height: 100))
addChild(sprite)
//3. Create the action sequence from previously created actions
let sequence = SKAction.sequence(actions)
//4. Run the sequence (use the key to stop this sequence)
sprite.run(sequence, withKey:"aKey")
}
}
【讨论】:
CGMutablePath 在幕后做了什么)。