2

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?

user594883
  • 1,329
  • 2
  • 17
  • 36
  • print trailEmitter.position after this line : `trailEmmiter!.position = CGPointMake(((plane?.size.width)!/2),((plane?.size.height)!` and paste the result here. Also note that you shouldn't use forced unwrapping like that. It is unsafe. Better use optional binding instead. – Whirlwind Jun 13 '16 at 06:56
  • @Whirlwind I have three sprites so `print("trailEmitter!.position: \(trailEmitter!.position)")` is executed three times with the following results: trailEmitter!.position: (263.0, 213.0) trailEmitter!.position: (374.0, 155.0) trailEmitter!.position: (209.0, 74.0) – user594883 Jun 13 '16 at 12:54
  • Note that when you add a child node to its parent, it will be added to parents coordinate system. So what you want is to set its emitters location relative to its parent... Just for test, set CGZeroPoint for emitters position. That will place them at exact position where their parent is. – Whirlwind Jun 13 '16 at 12:58
  • @Whirlwind result: `trailEmitter!.position: (0.0, 0.0) trailEmitter!.position: (0.0, 0.0) trailEmitter!.position: (0.0, 0.0)`but the emitters are still not visible on screen. – user594883 Jun 13 '16 at 13:05
  • Well that is odd. Okay, are able to see if parent nodes are visible? Because if you see the parent, you have to see the emitter node if its position is (0,0). – Whirlwind Jun 13 '16 at 13:08
  • @Whirlwind Yes, the parent nodes, i.e. the `spaceShip: SKSpriteNode?`are visible on screen. – user594883 Jun 13 '16 at 13:11
  • You may upload a repo on the GitHub which can reproduce the issue you are talking about so hopefully somebody (or me, later on today) will take a look at it and find what the problem is. – Whirlwind Jun 13 '16 at 13:14
  • @Whirlwind thanks for your help but I managed to figure this one out. I had set scale for the sprite to 0.1 which made the emitter virtually invisible. – user594883 Jun 13 '16 at 18:12
  • No problem, you're welcome, glad to hear it you've sorted it out. – Whirlwind Jun 13 '16 at 19:01
  • If you want to add the trail behind the ship. First you make the zPosition of your ship. Second you add the rail on ship by making the ship parent of trail! Like ship.addChild(trailEmmiter2). Definitely it will solve your problem. – Raksha Saini Jun 14 '16 at 06:20

1 Answers1

2

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

Raksha Saini
  • 604
  • 12
  • 28