【问题标题】:SpriteKit - Animate SKShapeNode Shape PathSpriteKit - 动画 SKShapeNode 形状路径
【发布时间】:2018-12-21 17:54:19
【问题描述】:

目前,我正在使用 Core Graphics 制作形状,并使用 CAShapeLayerCABasicAnimation 路径动画将形状转换为不同的形状。

但是由于游戏的复杂性,如果要进入 SpritKit,使用 SKShapeNode,(放弃 CAShapeLayer 并使用 SKShapeNode)我能否将其平滑地动画成不同的形状?

我没有看到可以让您更改SKShapeNode 路径的SKAction 方法。

提前致谢。

【问题讨论】:

  • 你的身材怎么样?你想创造什么?您对CAShapeLayerSKShapeNode 进行了哪些尝试?
  • @Kamran CAShapeLayer 具有复杂的形状。大约200+点。我可以使用 CGPath 使用 SKShapeNode 重新创建形状。我碰到的墙是我必须为形状变化设置动画。
  • 我相信你可以通过一系列动作来做到这一点,一些淡入淡出,最后添加你想要转换的形状。您不需要使用 CAShapeLayer 或任何 CoreAnimation。

标签: ios swift xcode sprite-kit skshapenode


【解决方案1】:

嗯。节日快乐。

最简单的方法是使用 CALayerAnimation 来教 SKNode 如何像这样操作:

class ViewController: UIViewController {


@IBOutlet weak var skview: SKView!
var  path1: CGPath!
var  path2: CGPath!

override func viewDidLoad() {
    super.viewDidLoad()

  path1  = CGPath.init(rect: CGRect.init(x: 0, y: 0, width: 100, height: 100), transform: nil)

    path2 = CGPath.init(rect: CGRect.init(x: 0, y: 0, width: 250, height: 400), transform: nil)

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let shapeLayer = CAShapeLayer()
    self.view.layer.addSublayer(shapeLayer)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.path = path1

    let anim = CABasicAnimation.init(keyPath: "path")
    anim.duration = 1.0
    anim.fromValue  = path1
    anim.toValue = path2
    anim.isRemovedOnCompletion  = false


    let shapeNode = SKShapeNode.init(path: path1)
    shapeNode.fillColor = UIColor.green

    skview.scene?.addChild(shapeNode)


    shapeLayer.add(anim, forKey:
    "prepanimation")
    shapeNode.run(SKAction.customAction(withDuration: anim.duration, actionBlock: { (node, timeDuration) in
        (node as! SKShapeNode).path =
        shapeLayer.presentation()?.path
    }))

}
}

如果你的路径太大,一个最佳的方法是考虑将 CABasicAnimation 转换为 CAKeyFrameAnimation 方法。

从上述过程中,您可以在设计时提取一对(时间,presentation_Path)。然后在运行时在 SKCustomAction 中重新分配。请参考 SKKeyframeSequence 来了解这个想法(不完全是类似的动画)。

【讨论】:

  • 我的意思是远离“CAShapeLayer”并使用 SKShapeNode 代替它。有没有办法做到这一点?上面看起来不错,但似乎效率不高,因为 CAShapeLayer 和 SKShapeNode 完成了两次工作。
  • 你必须为此编写自定义动画。
  • 我提供了全部赏金,因为您的解决方案看起来很聪明。但是,我仍在寻找这个动画要严格在 SpriteKit 上完成。
  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多