Game Development Reference
In-Depth Information
The next most important thing to do for our InvinciBagel game is to make sure that the
character does not disappear off the edge of the screen by placing a .setBoundaries()
method call between the .setXYLocation() method, which evaluates arrow (or ASDW)
keypress combinations, and increments the iX and iY Bagel object properties accord-
ingly, and the .moveInvinciBagel() method, which actually executes the movement. By
placing the .setBoundaries() method before the sprite movement is invoked, we can
make sure that the sprite is not off the screen (and if he is, move him back onto the
screen) before we actually call the move function (method). The first step in writing
this code is to define the sprite size in pixels so that we can calculate this along with
our WIDTH and HEIGHT Stage size constants to determine the boundary variable val-
ues that we will need to check our iX and iY sprite location against inside of the
.setBoundaries() method and its conditional if() statement structures. As you can see in
Figure 12-15 , I define these sprite pixel size constant declarations at the top of the Ba-
gel.java class, by using the following two lines of Java code:
protected static final double SPRITE_PIXELS_X = 81;
protected static final double SPRITE_PIXELS_Y = 81;
Figure 12-15 . Declare protected static final double SPRITE_PIXELS_X and SPRITE_PIXELS_Y constants at the top
of class
Next, we need to calculate the four screen boundary values using the WIDTH and
HEIGHT constants in the InvinciBagel class and the SPRITE_PIXELS_X and
SPRITE_PIXELS_Y constants we just defined at the top of this class. As you may
have noticed from our 0,0 initial X,Y Bagel object location coordinates putting our
sprite in the center of the screen, JavaFX is using a centered X axis and Y axis screen
addressing paradigm. This means there are four quadrants, and that negative values
(which mirror positive values) move left and up, and positive values move right and
down. We can actually use this paradigm later on to quickly ascertain which quadrant
 
 
Search WWH ::




Custom Search