如何在序列中添加和删除 SKErmitterNode?

How to add and remove a SKErmitterNode within a sequence?

我正在从 update() 函数中添加一个 SKErmitterNode。我想在 2 秒后删除 SKErmitterNode,因此我制作了一个序列,但在序列中,我无法添加节点。如果我在序列之外添加节点,它会被一遍又一遍地添加(因为我在更新函数中做了所有这些)有人知道更好的方法吗?

这是我的更新函数代码:

 override func update(_ currentTime: CFTimeInterval) {

    if player.position.y <= player.size.height / 2{

        self.player.removeFromParent()

        if let particles = SKEmitterNode(fileNamed: "MyParticle.sks") {
            particles.position = player.position

            let addParticle = addChild(particles)
            let wait = SKAction.wait(forDuration: 2.0)
            let removeParticle = SKAction.removeFromParent()
            let particleSequence = SKAction.sequence([addParticle, wait, removeParticle]) //Error ->Cannot convert value of type 'Void' to expected element type 'SKAction'
            self.run(SKAction.run(particleSequence))

        }
    }

所以我建议你做的是创建一个像下面这样的函数

func myExplosion (explosionPosition: CGPoint){
    let explosion = SKEmitterNode(fileNamed: "MyParticle")// borrowed this from you
    explosion?.position = explosionPosition
    explosion?.zPosition = 3
    self.addChild(explosion!)
    self.run(SKAction.wait(forDuration: 2)){//you can always change the duration to whatever you want
        explosion?.removeFromParent()
    }
}

然后该用这个功能的时候就这样用

myExplosion(explosionPosition: player.position)

希望这对您有所帮助。