0

In my game, I have a Player and a Mine. When the player hits the mine, I add an SKEmitterNode to the scene to simulate an explosion.

In the Mine class, I declare an instance variable for the SKEmitterNode like this:

var particles = SKEmitterNode(fileNamed: "MineExplosion")!

and when the player touches the mine, I call a function in the Mine class called explode that adds the emitterNode to the scene like this:

self.particles.position = self.position
foreGround.addChild(self.particles)

The problem is that the particles are never added to the scene. However, if I create the SKEmitterNode inside the explosion function, then add it to the scene, the particles appear:

let particles = SKEmitterNode(fileNamed: "MineExplosion")!
particles.position = self.position
foreGround.addChild(particles)

So the only difference is declaring the SKEmitterNode during the init of the Mine, or on the fly during the explosion function. I'd like to declare it during the init in case creating the SKEmitterNode is what's causing my game to jitter when hitting a mine. Has anyone else experienced this?

claassenApps
  • 1,137
  • 7
  • 14
  • 1
    "I declare a class variable" No you don't. That's an instance variable, not a class variable. – matt Sep 06 '16 at 22:39
  • my bad. changed it in the question. – claassenApps Sep 06 '16 at 22:40
  • In the second part you change the position, have you set the position also for the first part with the same code? – Alessandro Ornano Sep 07 '16 at 06:37
  • When I try to use the instance variable, I use self.particles instead of just particles. – claassenApps Sep 07 '16 at 11:41
  • What is `foreGround`? – Eimantas Sep 07 '16 at 12:42
  • foreGround is an SKSpriteNode that holds all my level objects. I pass it in to the explosion function so I can add the SKEmitterNode to it. It is declared in my GameScene class, which contains most of the logic for the game. I'm pretty sure the issue is not with foreGround since creating the SKEmitterNode and adding it to foreGround on the fly works as intended. – claassenApps Sep 07 '16 at 12:45
  • 1
    sounds like your emitter may have already emitted, when are you adding it to the foreground, at the time of explosion? – Knight0fDragon Sep 07 '16 at 12:57
  • That's a good thought. I'm adding the SKEmitterNode to the foreGround at the time of the explosion. The particles should start to emit once they're added to the scene though, right? – claassenApps Sep 07 '16 at 14:25
  • I think is already emitted as @Knight0fDragon said. In the documentation: Consider removing a particle emitter from the scene when it is not visible onscreen. **Add it just before it becomes visible** – Simone Pistecchia Sep 07 '16 at 16:05
  • It's not added to the scene until the explosion happens, so I do add it right before it becomes invisible. – claassenApps Sep 07 '16 at 16:07
  • you can try to set infinite emission and set the range of birth rate bigger to see if you are in wrong position – Simone Pistecchia Sep 07 '16 at 16:13
  • After setting the emissions to infinite, the particles do show up properly and in the right position, so it seems that they are being emitted earlier than expected. Instantiating the SKEmitterNode shouldn't set off the emission, right? That's the only thing I can think of since that's the only bit of code that's changing... – claassenApps Sep 07 '16 at 16:49
  • ok, how fast is this explosion? I have noticed a delay with Emitter nodes when they first start – Knight0fDragon Sep 07 '16 at 18:05
  • The lifetime of the particles is 0.5 seconds. I also have an SKAction that will remove the SKEmitterNode from the scene after 0.55s. I've tested it with and without the SKAction to remove it and the particles don't show up either way. I'm curious about the delay because instantiating the SKEmitterNode right before adding it to the scene works every time, but instantiating it as an instance variable does not. – claassenApps Sep 07 '16 at 19:20
  • 1
    In apple botdemo, they create the var and after, when it's time to use that, copy the var and add it on the node like: ' let emitterCopy = particles.copy() as! SKEmitterNode ' – Simone Pistecchia Sep 08 '16 at 08:50
  • That does work, thank you. Does anyone know if there is any advantage to using the .copy() method rather than instantiating the entire SKEmitterNode during the explosion? – claassenApps Sep 08 '16 at 22:43

0 Answers0