Game Development Reference
In-Depth Information
often as you start creating your own games. After all, it is about your players
having fun, not matching the real world.
Applying Forces to SKPhysicsBody
At this point you have your player responding to the physical properties of your game
world, and you have also adjusted the gravitational forces that pull the player off the bot-
tom of the visible scene. This is great, but watching your character fall of the screen is not
going to be a whole lot of fun. It is now time to apply some forces to the player to allow
him to fight gravity and stay in the visible scene. The two most common methods of chan-
ging the velocity of an SKPhysicsBody are applying a force and applying an impulse.
When applying a force to an SKPhysicsBody , you apply the force for a length of time
based on the amount of time that passes between invocations of the rendering loop. Forces
are generally used for continuous velocity changes. You apply forces using the SKPhys-
icsBody 's applyForce() method.
When you apply an impulse to an SKPhysicsBody , you are applying an instantaneous
change to the body's velocity that is independent of the amount of simulation time that has
passed. Impulses are used when you need to apply an immediate change to a node's velo-
city. You apply impulses using the SKPhysicsBody 's applyImpulse() method.
The game is going to use impulses to modify the player's velocity and will therefore use
the applyImpulse() method.
Before you can apply an impulse to the player's physics body, you need to give the user
the ability to tell the game when to apply the impulse. Because SKNode extends
UIResponder and SKScene is an SKNode , you can test for touches in the scene by
overriding the GameScene 's UIResponder.touchesBegan() method. To do this,
you need to complete two steps.
Add the following line of code that turns on user interaction in the scene immediately be-
fore the line of code that creates your backgroundNode :
userInteractionEnabled = true
And then override the UIResponder.touchesBegan() by adding the following
method to the GameScene class:
Search WWH ::




Custom Search