Game Development Reference
In-Depth Information
// add the player
playerNode = SKSpriteNode(imageNamed: "Player")
playerNode!.physicsBody =
SKPhysicsBody(circleOfRadius: playerNode!.size.width
/ 2)
playerNode!.physicsBody!.dynamic = true
playerNode!.position = CGPoint(x: size.width / 2.0, y: 80.0)
addChild(playerNode!)
Now, go back to Xcode and run the application once again. If you were not paying close
attention, you would see only the background node of the game. This is because the de-
fault gravity setting in Sprite Kit matches the earth's gravitational forces, and the player
node is now falling rapidly to the center of the earth.
To slow things down, you need to play around with the game world's gravity settings until
you have a gravitational force you are happy with. Go back to the GameScene.init
(size: CGSize) method and add the following line immediately following the su-
per.init(size: size) invocation; then run the application again:
physicsWorld.gravity = CGVectorMake(0.0, -0.1);
Now you will see the player slowly drift off the bottom of the screen. What you have done
here is modify the world's gravity using the SKScene 's physicsWorld.gravity
property and setting it to a value that slows the playerNode 's descent considerably.
Notice the value you set the gravity property to is a vector with a value of 0.0 for the x-co-
ordinate and a value of -0.1 for the y-coordinate. You set the x-coordinate to a value of 0.0
because gravity exerts force only along the y-axis. The result of this vector is a force that
results in a gravitational force pulling toward the bottom of the scene.
While setting the y-coordinate to -0.1 helps us to see the playerNode fall off the scene,
it is not practical for game play. A more reasonable value would be -2.0. Set the gravity
property to CGVectorMake(0.0, -2.0) and try running it again. You will see the
player fall off the screen, but it will be at a rate more conducive to game play.
Note In this section you modified the world's gravity property to a value that
was not consistent with the earth's real gravity. I had you do this because you
are not trying to match the earth's gravity but are instead trying to create a sim-
ulated game world that is fun for your players. You will find yourself doing this
Search WWH ::




Custom Search