菜鸟教程小白 发表于 2022-12-11 19:18:31

ios - 从 SKEmitterNode 动画粒子


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

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

<pre><code>            // Create animation textures
            let animationAtlas = SKTextureAtlas(named: atlasFilename)
            var animationFrames = ()

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

            // Load texture array
            for i in 0..&lt;numImages {
                let textureName = &#34;\(texturePrefix)\(i)&#34;
                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()

            // Add emitter node to scene
            addChild(emitterNode)
            emitterNode.run(sequence)
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>虽然您的代码看起来完美无缺,这可能是粒子引擎的一个错误,但值得假设您的动画在粒子变得足够大或足够可见之前用完帧,以便您看到动画执行。如果是这种情况,动画完成后纹理可能会恢复为您看到的原始纹理。</p>

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

<pre><code>repeatAction = SKAction.repeatForever(animation)
</code></pre>

<p>然后改变particleAction的赋值如下:</p>

<pre><code>emitterNode.particleAction = repeatAction
</code></pre>

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

<p>如本页 (<a href="https://developer.apple.com/reference/spritekit/skemitternode" rel="noreferrer noopener nofollow">https://developer.apple.com/reference/spritekit/skemitternode</a>) 所述,粒子显然不是可访问的 Sprite 节点,但同时它们的行为应该像 Sprite :</p>

<p>“为了对粒子使用 Action ,您可以将粒子视为 Sprite 。这意味着您可以执行其他有趣的技巧,例如为粒子的纹理设置动画。”</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 SKEmitterNode 动画粒子,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42892909/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42892909/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 SKEmitterNode 动画粒子