Ios 如何在Sprite Kit-Swift中通过CGPoints数组设置动画
如何在swift中从Ios 如何在Sprite Kit-Swift中通过CGPoints数组设置动画,ios,swift3,sprite-kit,Ios,Swift3,Sprite Kit,如何在swift中从CGPoints数组中设置SKSpriteNode的动画?我还希望SKSpriteNode朝着下一个位置旋转。任何帮助都将不胜感激。您可以这样做: import SpriteKit class GameScene: SKScene,SKSceneDelegate { override func didMove(to view: SKView) { //1. create points let points = [CGPoint(
CGPoints
数组中设置SKSpriteNode
的动画?我还希望SKSpriteNode
朝着下一个位置旋转。任何帮助都将不胜感激。您可以这样做:
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")
}
}
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")
}
}
根据@KnightOfDragon的建议,您可以创建一条路径并使节点跟随它,如下所示:
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")
}
}
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")
}
}
class游戏场景:SKScene{
覆盖func didMove(到视图:SKView){
//1.创建点
设点=[
CGPoint(x:frame.minX,y:frame.minY),
CGPoint(x:frame.maxX,y:frame.maxY),
CGPoint(x:frame.maxX,y:frame.midY),
CGPoint.zero
]
//2.创建路径
let path=CGMutablePath()
//3.确定起点
移动路径(到:点[0])
//4.添加其他点
点对点[1..谢谢,有没有办法将SKSpriteNode朝着它移动的方向旋转?另外,我能为每个动画设定一个恒定的速度吗?看看这个动画的目标部分。要解决恒定速度问题,请使用公式“时间=距离/速度”。如果你卡住了,问另一个问题,我或其他人会回答,因为这是不同的你已经对你原来的问题有了答案…很惊讶你用这种方法而不是做一个CGPath@Knight0fDragon好吧,我想我只是在回答问题时付出了和OP一样的努力:)我不知道,这是我想到的第一件事,我在30秒内写了一个答案:)但是,是的,梅be your way更适合OP。它似乎更适合内存,但没有测试,谁知道呢;)(我的意思是我不知道CGMutablePath
在幕后做什么)。