Game Development Reference
In-Depth Information
First, we get the pointer to our player sprite. Then, we calculate the new position by
multiplying the accelerometer data and the out multiplier to get the sprite position
on the Y axis. As we don't want to have our character fly off screen, we add a check;
if it goes below or over a certain point, its vertical position is reset to the minimum
value. Build and run the project to see if everything works.
Physics engine
The next big topic that we will discover is physics in our application. Physics-based
games are very popular on the App Store. Angry Birds, Tiny Wings, and Cut the
Rope are all physics-based. They offer lifelike interactions that are fun and appealing.
Another reason why we might want to have a physics engine is that it offers a lot
of functionality "for free". You no longer need to calculate different complicated
collisions, forces, and all things that may affect your nodes. Most of these things are
handled for you by a physics engine.
Physics simulation basics
If you want your node to participate in physics simulation, you have to add a physics
body to it. There are many available methods to generate physics bodies, and they
are as follows:
BodyWithCircleOfRadius : This method creates a circular physics body.
It is the fastest physics body, and if you have many objects that need to
be simulated, consider setting their physics body to a circle.
BodyWithRectangleOfSize : This is the same as the previous one,
but with a rectangle. Usually, passing a frame of a node is sufficient
enough for most games.
BodyWithPolygonFromPath : This creates a physics body from CGPath.
This is useful if you have a very complex sprite and you want it to be
simulated as real as possible.
BodyWithEdgeFromPoint:toPoint : This is useful to create edges such
as ground level.
There are more methods available; you can check them in the SKPhysicsBody
class reference.
 
Search WWH ::




Custom Search