GS.
GS.

Reputation: 123

Spritekit Adding Particles

I want to make my spaceship have a particle trail that follows it. How would I do this?

Here's my code for my spaceship:

override func didMove(to view: SKView) {
    spaceS.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    spaceS.setScale(0.05)
    spaceS.zPosition = 1

    addChild(spaceS)

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches {
            let location = touch.location(in: self)
            spaceS.position = CGPoint(x: location.x, y: location.y)
        }


}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
               for touch in touches {
            let location = touch.location(in: self)
            spaceS.position = CGPoint(x: location.x, y: location.y)
        }


}

EDIT 1: A fire particle trail to be more exact

Upvotes: 1

Views: 600

Answers (1)

Joe
Joe

Reputation: 8986

First you need to create a SpriteKit Particle File by pressing command + n on keyboard and from the template under iOS -> Resources -> choose SpriteKit Particle File click Next and choose particle template to fire press next and give a name to SpriteKit Particle File this will create a yourFileName.sks file to your project.

Use the below code:

override func didMove(to view: SKView) {

    spaceS = SKSpriteNode(imageNamed: "Spaceship")
    spaceS.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    spaceS.setScale(0.50)
    spaceS.zPosition = 1

    addChild(spaceS)

    let emitter = SKEmitterNode(fileNamed: "MyParticle") //MyParticle is the name of the sks file.
    emitter?.position = CGPoint(x: 0, y: -spaceS.size.height)
    emitter?.zPosition = 1
    spaceS.addChild(emitter!) //Now the emitter is the child of your Spaceship.      
}

Output:

enter image description here

Upvotes: 2

Related Questions