OGeek|极客世界-中国程序员成长平台

标题: ios - 从 SKEmitterNode 动画粒子 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:18
标题: ios - 从 SKEmitterNode 动画粒子

目标是为来自 SKEmitterNode, 的粒子设置动画,但以下代码不起作用。粒子不会改变纹理。它们只显示生命周期中的第一个纹理——或者更具体地说,是 Xcode 粒子编辑器中使用的原始图像。

粒子生命周期比帧持续时间长,所以这不是问题。

            // Create animation textures
            let animationAtlas = SKTextureAtlas(named: atlasFilename)
            var animationFrames = [SKTexture]()

            // Set number of animation frames
            let numImages = animationAtlas.textureNames.count

            // Load texture array
            for i in 0..<numImages {
                let textureName = "\(texturePrefix)\(i)"
                animationFrames.append(animationAtlas.textureNamed(textureName))
            }

            // Create emitter node w/ animation on each particle
            let emitterNode = SKEmitterNode(fileNamed: EmitterFilename)!
            let animation = SKAction.animate(with: animationFrames, timePerFrame: 0.05)                
            emitterNode.particleAction = animation

            // Define fade out sequence
            let fadeOut = SKAction.fadeOut(withDuration: sparkleFadeOutDur)
            let remove = SKAction.removeFromParent()
            let sequence = SKAction.sequence([fadeOut, remove])

            // Add emitter node to scene
            addChild(emitterNode)
            emitterNode.run(sequence)



Best Answer-推荐答案


虽然您的代码看起来完美无缺,这可能是粒子引擎的一个错误,但值得假设您的动画在粒子变得足够大或足够可见之前用完帧,以便您看到动画执行。如果是这种情况,动画完成后纹理可能会恢复为您看到的原始纹理。

要检查这是否是问题所在,您可能需要将动画 Action 包装到 repeatForever Action 中:

repeatAction = SKAction.repeatForever(animation)

然后改变particleAction的赋值如下:

emitterNode.particleAction = repeatAction

值得注意的是,Apple 的文档似乎自相矛盾。在此页面 (https://developer.apple.com/reference/spritekit/skaction/1417828-animate) 我们看到:“此操作只能由 SKSprite​Node 对象执行。”

如本页 (https://developer.apple.com/reference/spritekit/skemitternode) 所述,粒子显然不是可访问的 Sprite 节点,但同时它们的行为应该像 Sprite :

“为了对粒子使用 Action ,您可以将粒子视为 Sprite 。这意味着您可以执行其他有趣的技巧,例如为粒子的纹理设置动画。”

关于ios - 从 SKEmitterNode 动画粒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42892909/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4