Game Development Reference
In-Depth Information
override func touchesBegan(touches: NSSet, withEvent event:
UIEvent) {
playerNode!.physicsBody!.applyImpulse(CGVectorMake(0.0,
40.0))
}
Add this method to the bottom of the GameScene class definition and rerun the Super-
SpaceMan application. While the application is running, tap the screen as many times as
you need to get the player to fly back into the scene. The number of taps it takes will de-
pend on how far the playerNode fell out of the scene.
After playing around with the touch responder, take a look at the single line of code in the
touchesBegan() method. This line applies an impulse to the playerNode 's physics
body every time you tap the screen. The direction and strength of the impulse depend
upon the vector you pass to the applyImpulse() method. In this case, you are creat-
ing a vector with an x-value of 0.0 (because you want to apply the impulse only linearly
along the y-axis) and a y-value of 40.0, which results in a pulse that springs the player in
the opposite direction of gravity.
Before moving on, play around with the y-value used to create this vector. It will help you
understand how this value affects the size of the pulse. When you are finished, put the y-
value back to 40.0 and let's move on.
Once you have the player visible in the scene, tap the screen until the player is at the top
of the visible scene and watch him fall. One thing to note about the way the player-
Node descends is that the player falls as if it has no surface area to dampen its velocity.
This does not look quite right.
To fix this problem, Sprite Kit's SKPhysicsBody provides a linearDamping prop-
erty. This property, which has a default of 0.1, is used to reduce a physics body's linear
velocity to simulate fluid or air friction. In this case, you are simulating air friction. To see
how you can use this property, add the following line of code immediately after the posi-
tioning of the playerNode and run the application again:
playerNode!.physicsBody!.linearDamping = 1.0
Now tap the screen until the player reaches the top of the screen and let it fall once more.
This time, the playerNode will fall more slowly, simulating the resistance of falling
through air.
Search WWH ::




Custom Search