Game Development Reference
In-Depth Information
private void updateBob( float deltaTime, float accelX) {
if (bob.state != Bob. BOB_STATE_HIT && bob.position.y <= 0.5f)
bob.hitPlatform();
if (bob.state != Bob. BOB_STATE_HIT )
bob.velocity.x = -accelX / 10 * Bob. BOB_MOVE_VELOCITY ;
bob.update(deltaTime);
heightSoFar = Math. max (bob.position.y, heightSoFar);
}
The updateBob() method is responsible for updating Bob's state. The first thing it does is check
whether Bob is hitting the bottom of the world, in which case Bob is instructed to jump. This
means that, at the start of each level, Bob is allowed to jump off the ground of our world. As
soon as the ground is out of sight, this won't work anymore, of course. Next, we update Bob's
horizontal velocity, which is based on the value of the x axis of the accelerometer we get as an
argument. As discussed, we normalize this value from a range of -10 to 10 to a range of -1 to
1 (full left tilt to full right tilt) and then multiply it by Bob's standard movement velocity. Next,
we tell Bob to update himself by calling the Bob.update() method. The last thing we do is keep
track of the highest y position Bob has reached so far. We need this to determine whether Bob
has fallen too far later on.
private void updatePlatforms( float deltaTime) {
int len = platforms.size();
for ( int i = 0; i < len; i++) {
Platform platform = platforms.get(i);
platform.update(deltaTime);
if (platform.state == Platform. PLATFORM_STATE_PULVERIZING
&& platform.stateTime > Platform. PLATFORM_PULVERIZE_TIME ) {
platforms.remove(platform);
len = platforms.size();
}
}
}
Next, we update all the platforms in updatePlatforms() . We loop through the list of platforms
and call each platform's update() method with the current delta time. If the platform is in the
process of pulverization, we check for how long that has been going on. If the platform is in the
PLATFORM_STATE_PULVERIZING state for more than PLATFORM_PULVERIZE_TIME , we simply remove
the platform from our list of platforms.
private void updateSquirrels( float deltaTime) {
int len = squirrels.size();
for ( int i = 0; i < len; i++) {
Squirrel squirrel = squirrels.get(i);
squirrel.update(deltaTime);
}
}
Search WWH ::




Custom Search