SKEmitterNode isn't removing itself from parent?
Asked Answered
S

2

6

I added SKEmitterNode as a childNode of my mainScene and than expected that It would be removed when particleLifetime ends, described like apple docs.

Added emitters like this;

    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 image below ShipEngine.sks

When I run it emitters aren't removed from screen.

Simulator

I don't know what to add more if you need more information please ask any help would be appreciated thanks.

Spool answered 30/7, 2015 at 16:37 Comment(0)
J
19

particleLifetime determines the average lifetime of a particle, in seconds. That doesn't affect on removal of SKEmitterNode from parent.

numOfParticlesToEmit which refers to Maximum field in Particles area of Particle Editor determines the number of particles that emitter should emit before stopping. That doesn't affect on removal of SKEmitterNode from parent too. Also note that you've set 0 in this field which will enable infinitely emitting.

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

  • start an emitter
  • wait for some duration of time
  • and remove the emitter from parent (at this point emitter should finish with emitting)

Here is an simple example to show you how to do this with 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 (note how node's count is changing after emitting is done) :

Removing emitters from scene

Hope this helps

EDIT:

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

emitter setup

The point is to use Color Ramp, and to choose Add for a blend mode.

Here is the dropbox link to the .sks file : Effect.sks

Jellicoe answered 30/7, 2015 at 18:30 Comment(7)
A more accurate emitter duration would be something like this: let duration = Double(emitter.numParticlesToEmit) / Double(emitter.particleBirthRate) + Double(emitter.particleLifetime + emitter.particleLifetimeRange/2)Mariammarian
This is a very cool SKEmitterNode animation! Would you mind sharing the SKS file (or explain how to recreate this)?Lederman
@Lederman I have tried to re-create it and I guess I got something that can give you a basic idea how to get similar effect. Sorry, it was two years ago, and I forgot what was the exact setup.Jellicoe
Thanks! Also how did you get it to create the semi-structured clockwise animation? That's the other interesting bit.Lederman
@Lederman Well those are multiple emitter nodes used right there. You can create them programmatically if you want emitters placed exactly on a circumference. That is pretty much easy task. In this example, I just put them randomly on touch :)Jellicoe
Ahhh the clockwise motion was not programmatic nor a single emitter node ... I thought you were creating this effect with a single emitter node, Still very cool! :)Lederman
@Lederman I think you may find interesting to see this (one emitter node solution) : stackoverflow.com/a/34927287Jellicoe
H
0

Set your Particle "BirthRate" and "Maximum" both to 20. Setting the max to 0 will repeat the birth.

image

Hickie answered 10/12, 2015 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.