Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node's path particle effect with SpriteKit

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)
    }
}
like image 801
Drakalex Avatar asked Dec 13 '14 13:12

Drakalex


1 Answers

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

like image 51
Dion Larson Avatar answered Oct 10 '22 01:10

Dion Larson