Game Development Reference
In-Depth Information
listener.jump();
if (rand.nextFloat() > 0.5f) {
platform.pulverize();
}
break ;
}
}
}
}
In the checkPlatformCollisions() method, we test for overlap between Bob and any of the
platforms in our world. We break out of that method early in case Bob is currently on his way
up. That enables Bob to pass through platforms from below. For Super Jumper, that's good
behavior; in a game like Super Mario Brothers , we'd probably want Bob to fall down if he hits a
block from below.
Next, we loop through all platforms and check whether Bob is above the current platform. If he
is, we test whether his bounding rectangle overlaps the bounding rectangle of the platform. If
it does, we tell Bob that he hit a platform, via a call to Bob.hitPlatform() . Looking back at that
method, we see that it will trigger a jump and set Bob's states accordingly. Next, we call the
WorldListener.jump() method to inform the listener that Bob has just started to jump again.
We'll use this later on to play back a corresponding sound effect in the listener. The last thing
we do is fetch a random number and, if it is above 0.5, tell the platform to pulverize itself. It will
be alive for another PLATFORM_PULVERIZE_TIME seconds (0.8) and will then be removed in the
updatePlatforms() method shown earlier. When we render that platform, we'll use its state time
to determine which of the platform animation keyframes to play back.
private void checkSquirrelCollisions() {
int len = squirrels.size();
for ( int i = 0; i < len; i++) {
Squirrel squirrel = squirrels.get(i);
if (OverlapTester. overlapRectangles (squirrel.bounds, bob.bounds)) {
bob.hitSquirrel();
listener.hit();
}
}
}
The method checkSquirrelCollisions() tests Bob's bounding rectangle against the bounding
rectangle of each squirrel. If Bob hits a squirrel, we tell him to enter the BOB_STATE_HIT state,
which will make him fall down without the player being able to control him any further. We also
tell the WorldListener about it so that Bob can play back a sound effect, for example.
private void checkItemCollisions() {
int len = coins.size();
for ( int i = 0; i < len; i++) {
Coin coin = coins.get(i);
if (OverlapTester. overlapRectangles (bob.bounds, coin.bounds)) {
coins.remove(coin);
len = coins.size();
Search WWH ::




Custom Search