Game Development Reference
In-Depth Information
Now that you have a place to store the emitter, let's load it with the values stored in the
EngineExhaust.sks
file. The following lines do this:
let engineExhaustPath =
NSBundle.mainBundle().pathForResource("EngineExhaust",
ofType: "sks")
engineExhaust =
NSKeyedUnarchiver.unarchiveObjectWithFile(engineExhaustPath!)
as? SKEmitterNode
engineExhaust!.position =
CGPointMake(0.0, -(playerNode!.size.height / 2))
You have seen these lines before, with the exception of the last line. This line sets the pos-
ition of the emitter to a point at
(0.0, -(self.playerNode!.size.height /
2))
. I am using this point because the emitter will need to be added to the
player-
Node
, and the
playerNode
's anchor point is the middle of the player. Using the negat-
ive value of half the size of the
playerNode
will place the emitter at the bottom of the
playerNode
. Go ahead and add these lines to the end of the
GameS-
cene.init(size: CGSize)
method.
OK, so far you have the emitter node loaded, and you are ready to add it the
player-
Node
. You can do this with the following two lines:
playerNode!.addChild(engineExhaust!)
engineExhaust!.hidden = true;
As you look over this change, you will see that the code is pretty straightforward. First the
engineExhaust
emitter node is added to the
playerNode
, and then the exhaust is
hidden. I am hiding the exhaust here because I want the exhaust emitter to be visible only
when the player of the game taps the screen. Add this code right after the line that sets the
position of the
engineExhaust
node.
There is one more thing you need to do, which is to make the
engineExhaust
visible
when the user taps the screen. To do this, add the following line of code to the end of the
second
if
statement in the
touchesBegan()
method and run the game again:
engineExhaust!.hidden = false