如何动画矩阵一个接一个地改变精灵?

时间:2016-03-21 16:32:34

标签: swift animation matrix sprite-kit action

我正在制作一个小游戏,因为我有一个矩阵组合SKSpriteNode和数字,当游戏结束我试图制作动画时,我重新检查矩阵按顺序逐个更改精灵数字。看 Board(方块在Sknode中,其他Sknode中的数字)

想法是将精灵改为其他颜色并在更改下一个之后等待2秒,但我无法做到。我不知道如何逐个更改精灵。我使这个函数“RecoverMatrix()”,这改变了精灵但是一下子就好了,好像不等待,他改变了所有的精灵,然后等待了2秒。

    Storyboard storyboard = new Storyboard();
    DoubleAnimation rotateAnimation = new DoubleAnimation()
    {
        From = 0,
        To = 360,
        Duration = new Duration(TimeSpan.FromSeconds(10.0))
};
    Storyboard.SetTarget(rotateAnimation, my_grid);
    Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

    storyboard.Children.Add(rotateAnimation);
    storyboard.Begin();

所以,我需要帮助,我找不到制作这个动画的方法。我非常感谢你的帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

这是你可以同时在每个节点上运行一个动作的方法(使用循环遍历所有的tile):

class GameScene: BaseScene, SKPhysicsContactDelegate {
  var blocks: [[SKSpriteNode]] = []

  override func didMoveToView(view: SKView) {
    makeBoard(4, height: 4)

    colorize()
  }

  func makeBoard(width:Int, height:Int) {
    let distance:CGFloat = 50.0

    var blockID = 1

    //make a width x height matrix of SKSpriteNodes
    for j in 0..<height {
      var row = [SKSpriteNode]()

      for i in 0..<width {
        let node = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 30, height: 30))
        node.name = "\(blockID++)"

        if let nodeName = node.name {node.addChild(getLabel(withText: nodeName))}
        else {
          //handle error
        }

        node.position = CGPoint(x: frame.midX + CGFloat(i) * distance,
          y: frame.midY - CGFloat(j) * distance )

        row.append(node)

        addChild(node)
      }

      blocks.append(row)
    }
  }

  func colorize() {
    let colorize = SKAction.colorizeWithColor(.blackColor(), colorBlendFactor: 0, duration: 0.5)
    var counter = 0.0
    let duration = colorize.duration

    for row in blocks {
      for sprite in row {
        counter++

        let duration = counter * duration
        let wait = SKAction.waitForDuration(duration)

        sprite.runAction(SKAction.sequence([wait, colorize]))
      }
    }
  }

  func getLabel(withText text:String) -> SKLabelNode {
    let label = SKLabelNode(fontNamed: "ArialMT")
    label.fontColor = .whiteColor()
    label.text = text
    label.fontSize = 20
    label.horizontalAlignmentMode = .Center
    label.verticalAlignmentMode = .Center

    return label
  }
}

结果:

enter image description here

所以基本上,正如我在评论中所说的那样,你可以在同一时刻运行所有动作,这就是每个动作何时开始。

答案 1 :(得分:0)

您似乎认为runAction(waiting)意味着代码暂停并等待,在循环之间暂停。它没有(实际上没有办法做到这一点)。您的代码会立即遍历所有循环,现在 KABOOM

因此,所有操作都会立即配置并一起执行。