2

I have a SKEmitterNode and I'm trying to stop it when a button is pressed. I add my node in this way:

let followLine = SKAction.followPath(border.CGPath, asOffset: false, orientToPath: true, duration: 2.0)
let loopAction = SKAction.repeatActionForever(followLine)
emitterNode.targetNode = scene
emitterNode.runAction(loopAction, withKey: "loop")
addChild(emitterNode)

I add the emitterNode to my SKScene and when I want to stop the particles I tried all these possible ways:

let action = SKAction.runBlock { [weak self] in
    self?.emitterNode.particleBirthRate = 0
}
emitterNode.runAction(action)


emitterNode.removeAllActions()
emitterNode.removeFromParent()


removeAllActions()


let remove = SKAction.removeFromParent()
emitterNode.removeActionForKey("loop")
emitterNode.runAction(remove)

The emitter doesn't stop and the animation continues.

Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43
  • Are you testing this on the Simulator or you use an actual device ? Because I get very different results based on what I use (device vs sim) and based on what iOS version I use. – Whirlwind Feb 09 '16 at 21:09
  • can you post a link to your particle emitter.. along with any code I need to get this running on my end.. like the border.CGPath variable. If you do that I can give it a shot. – hamobi Feb 09 '16 at 21:24
  • @Whirlwind I'm testing on Simulator and with iOS 9.3 beta 2 and Xcode 7.3 beta 2. – BalestraPatrick Feb 11 '16 at 16:40
  • @hamobi I just edited the original post and included a link to the particle emitter and the image file. You can set any border.CGPath of any UIView you have. I only have a single UIView in my case. Let me know if you need anything else. – BalestraPatrick Feb 11 '16 at 16:51
  • @BalestraPatrick Note that you should always test things on a real device if you are interested in real results. Anyways, I am able to stop the particles emitting on both Simulator and device (I am on iOS9.1 and Xcode 7.1.1) It may be worth of mentioning that I've run into another issue related to emitter's targetNode property, but still I am able to stop emitting either by removing emitter from its parent, or by setting particleBirthRate property to zero. – Whirlwind Feb 11 '16 at 22:49
  • Maybe it has to do with him using the new beta version of Xcode? – hamobi Feb 12 '16 at 06:37
  • @Whirlwind I tested on the device and I have the same result. Can you please post the exact code you're using? – BalestraPatrick Feb 12 '16 at 16:51
  • @BalestraPatrick Comments are not suitable for that. I create an emitter using SKNode's fileNamed convenience init, set emitter's targetNode property, create circular path using `CGPathCreateWithEllipseInRect` and run action like you are doing. In touchesBegan, I stop emitting using particleBirthRate = 0 (removing emitter from the scene works as well...). If you want, feel free to send me an email (it is in my profile) so I can respond back with a code I use. – Whirlwind Feb 12 '16 at 18:32

1 Answers1

0

I figured out it was an issue with my code. I was trying to stop an emitter node which was created in a computer property and so was allocated very time it was accessed. The instance wasn't obviously the same and the emitter node wasn't stopping. This is my tip. Don't confuse the syntax for computer properties with the syntax to initialize a property with a closure. These two pieces of code are very different:

// Created only once
var laserButton: ParticlesLoadingButton = {
    let button = ParticlesLoadingButton(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
    button.particleEffect = .Laser
    button.particleColor = UIColor.orangeColor()
    return button
}()

// Created every time it is accessed
var laserButton2: ParticlesLoadingButton {
    let button = ParticlesLoadingButton(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
    button.particleEffect = .Laser
    return button
}
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43