简体   繁体   English

在Swift中组织动画

[英]Organising animations in Swift

I have a number of different scenes in Swift. 我在Swift中有很多不同的场景。 Each scene animates a number of objects briefly. 每个场景都会简短地对多个对象进行动画处理。 These objects are disparate and all animate in their own unique ways. 这些对象是完全不同的,并且都以各自独特的方式进行动画处理。

The issue is that now I have lines and lines of code for each scene, all of which govern a different SKAction. 问题是,现在我每个场景都有几行代码,所有这些代码都控制着不同的SKAction。 Is there a way to minify this kind of code, or to restructure the code so that we put it all in one place (and draw from it somewhere else)? 有没有办法减少这类代码或重组代码,以便我们将所有代码放在一个地方(并从其他地方提取)?

One of my ideas is to make all of the animated nodes a specific class, like AnimatedNode, and then put all the animations in there; 我的想法之一是使所有动画节点成为一个特定的类,例如AnimatedNode,然后将所有动画放入其中。 as separate functions. 作为单独的功能。 This would effectively remove the code from the scene.swift file. 这将有效地从scene.swift文件中删除代码。 Is this the best I can do, or is there some other approach to this that I don't see? 这是我能做的最好的事情,还是有其他我看不到的方法?

EDIT: My animation code runs 100+ lines across every scene, but as an example of how animations are performed on one such node, this is what it looks like: 编辑:我的动画代码在每个场景中运行100多行,但是作为一个如何在一个这样的节点上执行动画的示例,这是它的样子:

// can
        let can = bgNode.childNode(withName: "can")
        let tip = SKAction.rotate(byAngle: CGFloat(GLKMathDegreesToRadians(10)), duration: 0.5)
        let tipSound = SKAction.playSoundFileNamed("pot", waitForCompletion: false)
        can?.run(SKAction.sequence([tip, tipSound, tip.reversed()]))

SKActions were meant to be re-used or at least pre-loaded. SKActions应该重新使用或至少预先加载。 Your idea is good, because cluttering your scenes with the same code over and over is poor design. 您的想法很好,因为一遍又一遍地用相同的代码弄乱场景是不好的设计。

You can make an ActionManager, that has properties for each action you want to run already pre-loaded as a property.. this will increase performance of your game, especially if you are using it more than once. 您可以制作一个ActionManager,该属性具有您要运行的每个动作的属性,这些属性已经预先加载为属性。这将提高游戏的性能,特别是如果您多次使用它。

There are multiple ways to do this, but a plain function (though not as performant as properties) is a good way to at least organize your code. 有多种方法可以执行此操作,但是普通函数(尽管性能不如属性)是至少组织代码的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM