Game Development Reference
In-Depth Information
We will hold the currently shown background in it. Next up, remove everything
inside the brackets of the initWithSize: method, create a new background there,
and add it as a child node to the scene:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.currentBackground = [ERGBackground
generateNewBackground];
[self addChild:self.currentBackground];
}
return self;
}
Build and run the project now and you will see the background there.
Everything looks pretty good. But how do we get it to move?
We may go into the update: method and do it in the same quick and dirty way as
we did with the label—just move it by some amount of pixels each time it updates.
But we don't want a different speed of scrolling on different devices. That's why we
will implement the moving speed based on time and not on iterations per second.
To do this, we need to add a new property to ERGMyScene.h :
@property (assign) CFTimeInterval lastUpdateTimeInterval;
This will hold the time of the last update, and having this and currentTime (which we
get from the update: method), we can find the delta (difference) since the last update.
In your update: method, remove any old code and add this code:
CFTimeInterval timeSinceLast = currentTime - self.
lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
}
The preceding code is taken from Apple and it looks good enough for us. It calculates
the delta we need in timeSinceLast and handles this value if too much time has
passed since the last update (for example, you exit the application and came back to it
or took a call).
 
Search WWH ::




Custom Search