15

I'm working with Swift, Sprite Kit and Xcode 6,

I would like to create a particle effect in SpriteKit a little bit like the particles effects of the balls in the iOS game named "Duet", but I don't know how to proceed, I managed to create a particle effect, but not a particle like in this game who follows a node and draw the node's path...

Here is my code :

let firstCircle = SKSpriteNode(imageNamed: "Circle")
let particle = SKEmitterNode(fileNamed: "FirstParticle.sks")

override func didMoveToView(view: SKView)
{
    firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
    firstCircle.physicsBody?.affectedByGravity = false

    particle.targetNode = firstCircle

    addChild(firstCircle)
    addChild(particle)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{        
    for touch: AnyObject in touches
    {
        firstCircle.position = touch.locationInNode(self)
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
{
    for touch: AnyObject in touches
    {
        firstCircle.position = touch.locationInNode(self)
    }
}
Drakalex
  • 1,488
  • 3
  • 19
  • 39
  • 1
    Set the emitter node's `targetNode` property to the node you want it to follow –  Dec 13 '14 at 13:42
  • I already did it, the particle is following my node but there isn't any "path" effect, like a trail – Drakalex Dec 13 '14 at 14:13
  • Can you post your code? –  Dec 13 '14 at 15:42
  • I posted the essential part, not all of it because it's too long – Drakalex Dec 13 '14 at 15:51
  • what exactly is the problem –  Dec 13 '14 at 15:59
  • The particles appears on the node, but when I move the node there is no "dust particles", I don't know how to call that but I want the particles to trace the node's path. – Drakalex Dec 13 '14 at 16:24
  • In your scene's `didFinishUpdate` set the emitter's position to the position of the circle. Remove the one which sets the targetNode –  Dec 13 '14 at 19:34
  • 1
    I doubt that you can create that effect with particles. – 0x141E Dec 13 '14 at 20:02
  • Okapi, I tested what you said, but it doesn't make the result i wanted to, and @0x141E, if it's not possible with the particles effects, how could I manage to create an effect like the particles of the two balls in the game "Duet" ? – Drakalex Dec 14 '14 at 10:31
  • I have the same question. How can we create an effect like that in the game of Duet? – Noah Covey Mar 15 '16 at 22:25

1 Answers1

14

To achieve a Duet like effect you need particle to be a child of the trailed node and targetNode set to the parent scene. targetNode controls which node the particles are rendered as a child of.

When particle is a child of the trailed node, it will emit particles with the trailed node as the origin. Changing targetNode to the parent scene leaves already emitted particles behind as the trailed node moves.

This code should work but you may need to fine tune FirstParticle.sks.

let firstCircle = SKSpriteNode(imageNamed: "Circle")
let particle = SKEmitterNode(fileNamed: "FirstParticle.sks")

override func didMoveToView(view: SKView)
{
    firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
    firstCircle.physicsBody?.affectedByGravity = false

    particle.targetNode = self

    addChild(firstCircle)
    firstCircle.addChild(particle)
}

I was able to get a similar effect and ended up creating a Playground to demonstrate it. Check it out here.

Demo of Duet trail effect

Dion Larson
  • 868
  • 8
  • 14