Game Development Reference
In-Depth Information
Since we are going to be adding more and more Java programming logic into the .up-
date() method of the Bagel class during the remainder of the topic, I want to put into
place some “method modularization” that will be quite similar to what we did for the
InvinciBagel class in Chapter 11 when we added the six new logical method structures.
Since we will be performing a number of complex operations inside of the .update()
method as the game becomes more and more complex, it is logical that the .update()
method should contain calls to other methods that logically organize the tasks that we
will need to do on each frame, such as determining keys pressed (or not pressed), mov-
ing the InvinciBagel character, looking to see if he has gone off the screen (setting
boundaries), and eventually controlling his visual states, detecting collision, and apply-
ing physics effects. The first thing that I want to do is to “extract” the movement of the
sprite into a .moveInvinciBagel() method that will perform any translation transforms
that need to be implemented using a moveInvinciBagel(iX, iY); method call.
This means that we will have to create the private void moveInvinciBa-
gel(double x, double y){...} method structure and place the .setTrans-
late() method calls inside of it, replacing them in the .update() method with the
.moveInvinciBagel() method call. The basic Java code to perform these changes to the
Bagel.java class are shown in Figure 12-13 , and will look like this Java code:
@Override
public void update() {
if(invinciBagel.isRight()) { iX += vX }
if(invinciBagel.isLeft()) { iX -= vX }
if(invinciBagel.isDown()) { iY += vY }
if(invinciBagel.isUp())
{ iY -= vY }
moveInvinciBagel(iX, iY);
}
private void moveInvinciBagel (double x , double y ) {
spriteFrame.setTranslateX( x );
spriteFrame.setTranslateY( y );
}
 
Search WWH ::




Custom Search