SKEmitterNode not removing itself from parent?

I added SKEmitterNode as childNode of my mainScene and expected it to be removed when particleLifetime

finished, described as apple docs .

Added such emitters,

    var emitterPath : String = NSBundle.mainBundle().pathForResource("ShipFire", ofType: "sks")!
    var emitter : SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(emitterPath) as! SKEmitterNode

    emitter.position = position
    emitter.particleLifetime = 0.1;

    self.addChild(emitter)

      

My SKEmitterNode properties like below image ShipEngine.sks

When I run it, the emitters are not removed from the screen.

Simulator

I do not know what to add more, if you need more information please ask for help please rate.

+3


source to share


2 answers


particleLifetime defines the average particle lifetime in seconds. This does not affect the removal of the SKEmitterNode from the parent.

numOfParticlesToEmit , which refers to the Maximum Field field in the Particles Editor's Particles, determines the number of particles that the emitter should emit before stopping. This does not affect the removal of the SKEmitterNode from the parent. Also note that you have set this field to 0, which will allow infinite radiation.

So, if you want to remove a node from the parent when the emitter is done with emitting, you can set the number of particles to emit (the "Maximum in the area of ​​particles inside the editor" field) and run the SKAction sequence which will be:

  • start the emitter
  • wait a while
  • and remove the emitter from the parent (at which point the emitter should finish emitting)

Here's a simple example showing how to do this using the SKAction sequence:

class GameScene: SKScene {


    let emitter : SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(NSBundle.mainBundle().pathForResource("MyParticle", ofType: "sks")!) as SKEmitterNode
    override func didMoveToView(view: SKView) {

        self.backgroundColor = SKColor.blackColor()


    }

    func addEmitter(position:CGPoint){

        var emitterToAdd   = emitter.copy() as SKEmitterNode

        emitterToAdd.position = position

        let addEmitterAction = SKAction.runBlock({self.addChild(emitterToAdd)})

        var emitterDuration = CGFloat(emitter.numParticlesToEmit) * emitter.particleLifetime

        let wait = SKAction.waitForDuration(NSTimeInterval(emitterDuration))

        let remove = SKAction.runBlock({emitterToAdd.removeFromParent(); println("Emitter removed")})

        let sequence = SKAction.sequence([addEmitterAction, wait, remove])

        self.runAction(sequence)


    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {


        let touch: AnyObject? = touches.anyObject()

        let location = touch?.locationInNode(self)

        self.addEmitter(location!)



    }


}

      

And here is the result (notice how the node counter changes after the emit is made):



Removing emitters from the scene

Hope it helps

EDIT:

For those interested in how to make a similar effect as in the video above, try something like this:

emitter tuning

The point is to use Color Ramp and select Add for the blend mode.

Below is the link to the .sks file: Effect.sks

+14


source


Set your particle "BirthRate" and "Maximum" both to 20. Setting max to 0 repeats the birth.



image

0


source







All Articles