Game Development Reference
In-Depth Information
The update() method starts off by updating Bob's position and bounding shape based on
gravity and his current velocity. Note that the velocity is a composite of the gravity and Bob's
own movement due to jumping and moving horizontally. The next two big conditional blocks
set Bob's state to either BOB_STATE_JUMPING or BOB_STATE_FALLING , and reinitialize his state
time depending on the y component of his velocity. If it is greater than zero, Bob is jumping;
if it is smaller than zero, Bob is falling. We only do this if Bob hasn't been hit and if he isn't
already in the correct state. Otherwise, we'd always reset the state time to zero, which
wouldn't play nicely with Bob's animation later on. We also wrap Bob from one edge of the
world to the other if he leaves the world to the left or right. Finally, we update the stateTime
member again.
From where does Bob get his velocity apart from gravity? That's where the other methods
come in.
public void hitSquirrel() {
velocity.set(0,0);
state = BOB _ STATE _ HIT ;
stateTime = 0;
}
public void hitPlatform() {
velocity.y = BOB _ JUMP _ VELOCITY ;
state = BOB _ STATE _ JUMP ;
stateTime = 0;
}
public void hitSpring() {
velocity.y = BOB _ JUMP _ VELOCITY * 1.5f;
state = BOB _ STATE _ JUMP ;
stateTime = 0;
}
}
The method hitSquirrel() is called by the World class if Bob hits a squirrel. If that's the case,
Bob stops moving by himself and enters the BOB_STATE_HIT state. Only gravity will apply to Bob
from this point on; the player can't control him anymore, and he doesn't interact with platforms
any longer. That's similar to the behavior Super Mario exhibits when he gets hit by an enemy. He
just falls down.
The hitPlatform() method is also called by the World class. It will be invoked when
Bob hits a platform while falling downward. If that's the case, then we set his y velocity to
BOB_JUMP_VELOCITY , and we also set his state and state time accordingly. From this point on,
Bob will move upward until gravity wins again, making Bob fall down.
The last method, hitSpring() , is invoked by the World class if Bob hits a spring. It does the
same thing as the hitPlatform() method, with one exception; that is, the initial upward velocity
is set to 1.5 times BOB_JUMP_VELOCITY . This means that Bob will jump a little higher when hitting a
spring than when hitting a platform.
Search WWH ::




Custom Search