1

teaching myself spritekit while recouping from back surgery. Any help or instruction you may provide is greatly appreciated.

I have a function that adds a "monster" sprite with a child SKEmitter. I would like to update this emitter to "turn on" and "turn off" the SKEmitter. This functions as long as there is only a single sprite on the screen. The moment a second node is created from the function, the first one stops turning on and off.

It appears to me that the SKAction is only acting on the latest node. I just can't anything that would tell me how to fix this.

Example of what I see in the UI. SKEmitter On and Off

My code:

    flameThrowerSplash = SKEmitterNode(fileNamed: "flamesThrowerSplash.sks")!
    let newX = flameThrowerSplash.position.x - 475
    let newY = flameThrowerSplash.position.y + (zombie1.size.height * 0.7)
    let newLocation = CGPointMake(newX, newY)

    flameThrowerSplash.position = newLocation
    flameThrowerSplash.targetNode = currentScene

    let wait = SKAction.waitForDuration(0.5)
    let pauseEmitter = SKAction.runBlock({self.flameThrowerSplash.particleBirthRate = 0})
    let startEmitter = SKAction.runBlock({ self.flameThrowerSplash.particleBirthRate = 455})
    let wait2 = SKAction.waitForDuration(0.5)
    let run = SKAction.sequence([wait, startEmitter,wait2, pauseEmitter])

    zombie1.runAction(SKAction.repeatActionForever(run))
  • I am confused with this code, what is flamethrower assigned to? Your scene? assign the flame thrower to the zombie, and run the particles on the zombie, otherwise flameThrowerSplash is getting overwritten, and the previous flameThrowerSplash will only exist as a child of the scene, you will have no access to it, and instead, zombie 1 and zombie 2 will both control the newest instance of flameThrowerSplace – Knight0fDragon Apr 13 '16 at 00:22
  • my apologies, I only included the code that controlled the SKActions around changing the variables for the flamethrower. The full function which adds the "monster" and the flamethrower is exactly as you have it above. The issue is that when the second "monster" is added - you'll see that the first one is no longer switching on and off. If I had recorded a longer segment of video you would see that as each new monster is added, only the last one added is impacted by the SKaction to change the emitter on and off. (sorry for the confusion) and thank you for commenting – Undead-Earth .com Apr 13 '16 at 11:50
  • Did you even read my comment? I can see why @Hamobo threw you off, he just copied my comment without explaining, and did a poor job at it because his answer doesn't even fix the problem, read my entire comment, every new zombie you spawn controls the latest flame thrower. If i were you, make zombie a custom class and spawn a flame thrower inside of the class, If I has some more code I could show you how to properly do it in an answer, until that time it is going to be guesses. – Knight0fDragon Apr 13 '16 at 12:06
  • Many thanks, I copied and went back to edit it but I didn't realize there was a 5 minute timer on editing my comment. :-( I appreciate your feedback. I will work on the sub classing but will mark this as solved as I see the problem now. – Undead-Earth .com Apr 13 '16 at 12:32

1 Answers1

2

it doesnt look like you want flameThrowerSplash to be a scene property.

instead of this

flameThrowerSplash = SKEmitterNode(fileNamed: "flamesThrowerSplash.sks")!

just declare it as a new variable

let flameThrowerSplash = SKEmitterNode(fileNamed: "flamesThrowerSplash.sks")

then you can add it to each zombie independently.

you were reassigning it for each new zombie.. thats why it was disappearing.

hamobi
  • 7,940
  • 4
  • 35
  • 64
  • my apologies, I only included the code that controlled the SKActions around changing the variables for the flamethrower. The full function which adds the "monster" and the flamethrower is exactly as you have it above. The issue is that when the second "monster" is added - you'll see that the first one is no longer switching on and off. If I had recorded a longer segment of video you would see that as each new monster is added, only the last one added is impacted by the SKaction to change the emitter on and off. (sorry for the confusion) and thank you for commenting. – Undead-Earth .com Apr 13 '16 at 11:49
  • No, this is not the full answer, you are just making his flamethrower local and fail to address the rest of his code, which is going to other cause problems like compile errors and memory leaks – Knight0fDragon Apr 13 '16 at 12:08