Game Development Reference
In-Depth Information
This code uses the playerJumpForce variable, which sets it in Common.h , as well as
the gravity vector:
static NSInteger playerJumpForce = 8000000;
static NSInteger globalGravity = -4.8;
We set gravity to custom as the real world's gravity is too high, and this strips away
the fun and precise controls of our game.
To affect gravity, add the following statement in ERGMyScene.m in the
initWithSize: method:
self.physicsWorld.gravity = CGVectorMake(0, globalGravity);
In order to make everything work, we need to comment out or delete the old
methods that are in the way. Comment out or delete the following methods:
touchesBegan , touchesMoved , touchesEnded , and touchesCancelled . If you
still have other gesture recognizers such as the long press gesture recognizer in
the didMoveToView: method, remove them too.
After these changes, we have a really precise and fun way to control our character.
We can control the jump of the player sprite just by tapping on the screen for a
certain amount of time. This mechanism is used in many endless runners, and is both
fun and addictive. You have to not only react fast but also predict what can happen,
since applying force to the character does not change your position instantaneously
as your movement carries some momentum, so you have to plan accordingly.
Summary
In this chapter, we have learned how to use the touch controls and gesture
recognizers to control characters on the screen. We have found out how to
move sprites with touch and learned the basics of the physics engine in Sprite
Kit. Integrating the physics engine in your game is a great way to handle collision
detection and add many interesting behaviors without bloating your code.
In the next chapter, we will learn about animations, how to implement them,
and related details, such as textures and texture atlases.
 
Search WWH ::




Custom Search