Game Development Reference
In-Depth Information
How to do it...
In this section, we will learn about the basic components that are used to develop games.
We will also learn how to set game configurations, including the world settings such as
gravity and boundary.
1. The initial step is to apply the gravity to the scene. Every scene has a physics
world associated with it. We can update the gravity of the physics world in our
scene using the following line of code:
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
Currently we have set the gravity of the scene to 0, which means the bodies will be
in a state of free fall. They will not experience any force due to gravity in the
world.
2. In several games we also need to set a boundary to the games. Usually, the bounds
of the view can serve as the bounds for our physics world. The following code will
help us to set up the boundary for our game, which will be as per the bounds of our
game scene:
// 1 Create a physics body that borders the screen
SKPhysicsBody* gameBorderBody = [SKPhysicsBody
bodyWithEdgeLoopFromRect:self.frame];
// 2 Set physicsBody of scene to gameBorderBody
self.physicsBody = gameBorderBody;
// 3 Set the friction of that physicsBody to 0
self.physicsBody.friction = 0.0f;
In the first line of code we are initializing a SKPhysicsBody object. This object
is used to add the physics simulation to any SKSpriteNode . We have created the
gameBorderBody as a rectangle with the dimensions equal to the current scene
frame.
Then we assign that physics object to the physicsBody of our current scene
(every SKSpriteNode object has the physicsBody property through which
we can associate physics bodies to any node).
After this we update the physicsBody.friction . This line of code updates
the friction property of our world. The friction property defines the friction value
Search WWH ::




Custom Search