iosswiftsprite-kitskspritenodeskemitternode

swift how to add SKEmitterNode as a trail of smoke behind SKSpriteNode?


I'm trying to put a trail of smoke behind my spaceship. I have the smoketrail worked out as an emitterNode but I can't seem to attach the emitter to the spaceship.

I can't figure out why this doesn't work. I.e. the emitter doesn't show up on screen at all:

    var spaceShip: SKSpriteNode?
    spaceShip = SKSpriteNode(imageNamed: "ship1.png")
    spaceShip!.position = CGPoint(x: Int(arc4random_uniform(UInt32(self.size.width))), y: Int(arc4random_uniform(UInt32(self.size.height))))

    let trailEmmiter = SKEmitterNode(fileNamed: "trailParticle.sks")
    trailEmmiter!.name = "trailNode"
    trailEmmiter!.position = CGPointMake(((plane?.size.width)!/2), ((plane?.size.height)!/2))
    trailEmmiter!.zPosition = 100 //much over everything else
    spaceShip!.addChild(trailEmmiter!)
    self.addChild(spaceShip!)

but this works. I.e. it puts my trail of smoke at a random position on screen.

    let trailEmmiter2 = SKEmitterNode(fileNamed: "trailParticle.sks")
    trailEmmiter2!.name = "trailNode"
    trailEmmiter2!.position = CGPoint(x: Int(arc4random_uniform(UInt32(self.size.width))), y: Int(arc4random_uniform(UInt32(self.size.height))))
    trailEmmiter2!.zPosition = 100 //much over everything else
    self!.addChild(trailEmmiter2!)

How can I put the trail of smoke behind the spaceship?


Solution

  • First Create a Global object of SKEmitterNode! For XCode 6

    var sparkEmmiter:SKEmitterNode!
    

    Create Emitter Node Function!

    func addTrailToTwinkle(){
        let sparkEmmitterPath               =   NSBundle.mainBundle().pathForResource("PierrePath", ofType: "sks")!
        // PierrePath is the EmitterNode name!
        sparkEmmiter                        =   NSKeyedUnarchiver.unarchiveObjectWithFile(sparkEmmitterPath) as SKEmitterNode
        sparkEmmiter.position               =   CGPointMake(15, -15.0)
        sparkEmmiter.name                   =   "PierrePath"
        sparkEmmiter.zPosition              =   22.0
        sparkEmmiter.targetNode             =   self.scene 
        ship.addChild(sparkEmmiter)
        //Here ship is the Target node. where Trail Added.
    }
    

    Now Use function addTrailToTwinkle() to create trail behind the ship. When want to create trail.

    addTrailToTwinkle()
    

    Here the property of emitter node!

    Here Is the image of EmitterNode Properties