【问题标题】:Animating particles from a SKEmitterNode从 SKEmitterNode 动画粒子
【发布时间】:2017-08-11 02:24:04
【问题描述】:

目标是为来自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)

【问题讨论】:

    标签: ios animation sprite-kit skaction


    【解决方案1】:

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

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

    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) 所述,粒子显然不是可访问的精灵节点,但同时它们的行为应该像精灵:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多