Search code examples
objective-ciphoneswiftsprite-kitskemitternode

Couldn't make a particle follow a path in spriteKit


This is the animation in XCode SKEmitter editor (I want to achieve this on the iPhone) :

enter image description here

This is the animation on the iPhone (I don't want this animation):

enter image description here

Using this code:

    let sparkEmmiter = SKEmitterNode(fileNamed: "fireflies.sks")
    self.addChild(sparkEmmiter) // self is a SKScene

    var circle: CGPathRef? = nil
    circle = CGPathCreateWithEllipseInRect(CGRectMake(400, 200, 200, 200), nil)
    let followTrack = SKAction.followPath(circle!, asOffset: false, orientToPath: true, duration: 3.0)
    let followTrackForever = SKAction.repeatActionForever(followTrack)
    //sparkEmmiter.runAction(followTrackForever)
    sparkEmmiter.particleAction = followTrackForever;

This is the emitter settings:

enter image description here

I tried both runAction and particleAction by referring to this question, but it doesn't work as I wanted it to...

-----------------Update----------------------------------------------

Tried the solution mentioned by hamobi (still doesn't work) :

    //If I were you:
    // 1) I'd make a sprite and 
    let texture = SKTexture(imageNamed: "spark")
    let mySprite = SKSpriteNode(texture: texture)
    self.addChild(mySprite)

    // 2) add the emitter in your first example as a child.
    let sparkEmmiter = SKEmitterNode(fileNamed: "fireflies.sks")
    mySprite.addChild(sparkEmmiter)

    // 3) I'd set the emitters targetNode to the scene.
    sparkEmmiter.targetNode = self

    // 4) Then I'd just animate the sprite itself in an arc.
    var circle: CGPathRef? = nil
    circle = CGPathCreateWithEllipseInRect(CGRectMake(400, 200, 200, 200), nil)
    let followTrack = SKAction.followPath(circle!, asOffset: false, orientToPath: true, duration: 3.0)
    let followTrackForever = SKAction.repeatActionForever(followTrack)
    //sparkEmmiter.runAction(followTrackForever)
    sparkEmmiter.particleAction = followTrackForever;

enter image description here

-----------------Update 2----------------------------------------------

Got it! Thx to hamobi :D this is the result :D:D

enter image description here


Solution

  • If I were you I'd make a sprite and add the emitter in your first example as a child. I'd set the emitters targetNode to the scene. Then I'd just animate the sprite itself in an arc.

    EDIT:

    okay so the main thing you were missing is that you should forget about using particleAction. Make mySprite run the followTrackForever action.

    heres my code

    //If I were you:
    // 1) I'd make a sprite and
    let texture = SKTexture(imageNamed: "spark")
    let mySprite = SKSpriteNode(texture: texture)
    mySprite.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    self.addChild(mySprite)
    
    // 2) add the emitter in your first example as a child.
    let sparkEmmiter = SKEmitterNode(fileNamed: "fireflies.sks")
    mySprite.addChild(sparkEmmiter)
    
    // 3) I'd set the emitters targetNode to the scene.
    sparkEmmiter.targetNode = self
    
    // 4) Then I'd just animate the sprite itself in an arc.
    var circle: CGPathRef? = nil
    circle = CGPathCreateWithEllipseInRect(CGRectMake(100, 200, 200, 200), nil)
    let followTrack = SKAction.followPath(circle!, asOffset: false, orientToPath: true, duration: 3.0)
    let followTrackForever = SKAction.repeatActionForever(followTrack)
    mySprite.runAction(followTrackForever)
    

    screenshot of my particle

    enter image description here

    my particle in action

    enter image description here