Game Development Reference
In-Depth Information
Game loop
Unlike UIKit, which is based on events and waits for user input before performing
any drawing or interactions, Sprite Kit evaluates all nodes, their interactions,
and physics as fast as it can (capped at 60 times per second) and produces results
on screen. In the following figure, you can see the way a game loop operates:
update:
Evaluating actions
Simulating physics
Rendering scene
The update loop
First, the scene calls the update:(CFTimeInterval)currentTime method and sends
it the time at which this method was invoked. The usual practice is to save the time
of the last update and calculate the time that it took from the last update (delta) to
the current update to move sprites by a given number of points, by multiplying the
velocity of a sprite by delta, so you will get the same movement regardless of FPS.
For example, if you want a sprite to move 100 pixels every second, regardless of your
game performance, you multiply delta by 100. This way, if it took long to process
the scene, your sprite will move slightly further for this frame; if it is processed
fast, it will move just a short distance. Either way you get expected results without
complex calculations.
After the update is done, the scene evaluates actions, simulates physics, and renders
itself on screen. This process repeats itself as soon as it's finished. This allows for
smooth movement and interactions.
You will write the most essential code in the update: method, since it is getting
called many times per second and everything on screen happens with the code we
write in this method.
You will usually iterate over all objects in your scene and dispatch some job for each
to do, such as character moving and bullets disappearing off screen. The update:
method is not used in a template project, but it is there if you want to customize it.
Let's see how we can use it to move the Hello, World! label off the screen.
First, find where the label is created in the scene init method, and find this line:
myLabel.text = @"Hello, World!";
 
Search WWH ::




Custom Search