Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a trail behind my character in SpriteKit?

I'm using SpriteKit (with Xcode 6 and Swift) and I have a character on the screen that I move around with on screen joysticks, and I want a little trail to follow behind him. How do I do that?

enter image description here

How big would my image need to be, and what would it need to look like? Also what would I use in my code?

like image 456
dscrown Avatar asked Aug 18 '14 17:08

dscrown


1 Answers

You should take a look at SKEmitterNode; it will "emit" particles that you can use as your trail. You can design the look and feel of your particles right in Xcode by adding a "SpriteKit Particle File" to your project:

new particle file

You'd then load the particle file in to a new SKEmitterNode like so:

let emitter = SKEmitterNode(fileNamed: "CharacterParticle.sks")

Then you'll need to set the SKEmitterNode's targetNode property to your SKScene so that the particles it emits don't move with your character (i.e. they leave a trail):

emitter.targetNode = scene

Then add your emitter to your character's SKNode. Lets assume you have an SKNode for your character called character, in that case the code would simply be:

character.addChild(emitter)

Typically this sort of thing would be done in your scene's setup method (in Apple's SpriteKit template, it's usually in didMoveToView). It could also be done in your character's custom SKNode or SKSpriteNode class, if you have one. If you put it in didMoveToView, it would look something like:

override func didMoveToView(view: SKView) {
    // ... any character or other node setup ...

    let emitter = SKEmitterNode(fileNamed: "CharacterParticle.sks")
    emitter.targetNode = self
    character.addChild(emitter)

    // ... any other setup ...
}
like image 190
Mike S Avatar answered Nov 15 '22 14:11

Mike S