【问题标题】:How to animate through an array of CGPoints in Sprite Kit - Swift如何通过 Sprite Kit 中的 CGPoints 数组制作动画 - Swift
【发布时间】:2017-05-08 10:14:06
【问题描述】:

如何快速从CGPoints 数组中为SKSpriteNode 设置动画?我还希望SKSpriteNode 旋转到下一个位置。任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift3 sprite-kit


    【解决方案1】:

    根据@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 属性动画以便节点转向遵循路径)。

    【讨论】:

      【解决方案2】:

      你可以这样做:

      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")
      
          } 
      }
      

      【讨论】:

      • 谢谢,有什么方法可以让 SKSpriteNode 朝着它移动的方向旋转?
      • 另外,我可以让每个动画的速度恒定吗?
      • 看看这个answer的瞄准部分。要解决恒定速度,请使用公式时间 = 距离 / 速度。如果您遇到困难,请再问一个问题,我或其他人会回答它,因为这是不同的主题,您已经有了原始问题的答案...
      • 惊讶于您采用这种方法而不是制作 CGPath
      • @Knight0fDragon 好吧,我想我只是付出了与 OP 在他的问题中所做的一样多的努力 :) 我不知道,这是我想到的第一件事我在 30 秒内写了一个答案 :) 但是,是的,也许你的方式更适合 OP。此外,它似乎更明智,但没有测试,谁知道;)(我的意思是我不知道 CGMutablePath 在幕后做了什么)。
      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多