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)
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With