2

I'm playing around with SKEmitterNode in my own little game. I'm shooting a cannon ball and when the ball hits one of the boxes in the scene, the box gets destroyed and a particle effect with a simple texture "BOOM" is created to make a transition when the box disappears. The problem is, that when I start my emitter I expect it to start emitting the particles right away, but this is not the case and the particle is played after a short delay of 0.5-1s.

Here is my function for creating an emitter:

func createParticleEmitter(fileName: String) -> SKEmitterNode
{
    let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "sks")
    assert(path != nil, "Could not find file \(fileName)")

    let obj : AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithFile(path!)
    assert(obj != nil, "The emiter \(fileName) could not be created")

    return obj as! SKEmitterNode
}

Then on collision, this function is called, where position is the position of the dying object, and scene is the parent scene of the dying object:

func addOnDeathParticles(position: CGPoint, scene : SKNode)
{
    var emitter : SKEmitterNode = createParticleEmitter("boom")

    emitter.position = position

    let seq = [SKAction.waitForDuration(PARTICLE_DIE_DURATION,
               withRange:PARTICLE_DIE_DURATION_RANGE),
               SKAction.removeFromParent()]

    emitter.runAction(SKAction.sequence(seq))
    scene.addChild(emitter)
}

The action list is added to make the emitter disappear after PARTICLE_DIE_DURATION with a random value of PARTICLE_DIE_DURATION_RANGE.

As I understand it, as soon as the emitter is attached to the scene, it should start emitting particles. But as I mentioned at the beginning, this is not the case and the particles start appearing after 0.5-1 second. The particle created is just a texture that is scaling up and slowly rotating but there is no delay set (not even sure if you can do that).

Did anyone had the same behaviour or has a suggestion what I'm doing wrong? Glad for any help :)

Cheers, TK

Arbitur
  • 38,684
  • 22
  • 91
  • 128
James Takarashy
  • 299
  • 2
  • 14
  • 1
    Does the particle emit right away in your .sks file ? Maybe `advanceSimulationTime:` could help you ( https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKEmitterNode_Ref/#//apple_ref/occ/instm/SKEmitterNode/advanceSimulationTime: ) ? – lchamp Jun 15 '15 at 06:42
  • If you only view it in simulator you will see some lags because SpriteKit is optimized for device architecture and not the desktop. Test on an iPhone or iPod and you will see a difference in performance. – Arbitur Jun 15 '15 at 06:53
  • I really did test the emitter only with simulator till now, I will try it on my iPad and report back. Hope you are right :D – James Takarashy Jun 15 '15 at 08:05
  • 1
    OK, the speed of simulator vs iPad is nearly the same and both have the lag. What saved me is the "advanceSimulationTime" suggested by Ichamp. Could you pls post it as an answer so I can mark it as resolved? Thx a lot to everyone :) – James Takarashy Jun 15 '15 at 10:52

1 Answers1

0

The solution was setting the advanceSimulationTime to exactly 1.0 sec. I'm not entirely sure why this is the case, but I suppose that the creation "animation" takes up this time.

Anyway, case closed and thanks for the help to everyone, especially lchamp since he suggested the solution.

James Takarashy
  • 299
  • 2
  • 14