Game Development Reference
In-Depth Information
But this is unnecessarily complicated. What if we wanted to add platforms,
how would we handle that?
Generally speaking, the action system is useful if you know you need to do
a certain thing with your objects and nothing changes while executing them.
This way, actions are useful and very helpful, since doing something like that
in the update loop can be daunting.
Another powerful feature of actions is that they can be sequenced or repeated,
and you can make very complex movements and effects with them.
Adding infinite scrolling
Since having the background disappear is not the way it is meant to be, we will add
an infinite background. One way to do it is to load as large a background as the
memory allows and hope the player loses before reaching the end of the background
image. But there is a much better way. We will have relatively small background
segments, and when the background segment is going to end, we will create a new
one and attach it to the end of the current segment. When the old one goes off the
screen, it is removed and destroyed.
This way, we will have only two backgrounds in memory at any time, and this
makes it easier and simpler to manage.
First we need to adjust the zPosition property of the player and the Background
object. If we make a new background when the player is on screen, eventually it will
cover the player, as nodes that were made later are rendered on top of earlier ones.
Go to ERGPlayer.m , and in the init method, add self.zPosition = 10 . Do the
same for ERGBackground.m in the class method that generates the background by
setting backg.zPosition = 1 .
Nodes with a predetermined zPosition will always be rendered in the correct
order. Nodes with a higher zPosition will be rendered on top of nodes with a
lower zPosition .
Here is the new update: method that gives us infinite background:
-(void)update:(CFTimeInterval)currentTime {
CFTimeInterval timeSinceLast = currentTime - self.
lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
 
Search WWH ::




Custom Search