Game Development Reference
In-Depth Information
As you can see in Figure 11-12 , there are no error or warning highlights regarding
this code, and we're ready to move on and add the conditional programming logic that
will look at which of these four variables are set to true, or KeyPressed, and which are
set to false, or KeyReleased. Inside of these conditional statements we'll place the code
that will move the iX and iY (Actor location) variables, based on the vX and vY (Actor
velocity of movement) variables.
Figure 11-12 . Add import static invincibagel.InvinciBagel references to static boolean down, left, right, and up vari-
ables
Since we are writing this code inside of the Bagel object (in our case, we instanti-
ated and named it iBagel), we will have a chance to utilize and understand the iX and
iY variables, which we will be doing in the next section of this chapter, when we devel-
op the code statements that access and change the iBagel Bagel object's iX and iY loc-
ation attributes, as well as adding code that accesses and utilizes the iBagel Bagel ob-
ject's vX and vY velocity attributes.
Building the .update() Method: Using If Statements to
Determine X or Y Movement
Now it is time to add some basic Java programming logic inside of the Bagel class .up-
date() method that will move the iBagel object along the X or Y axis (or both, if mul-
tiple keys are being pressed). Since our iX and iY variables hold the Actor location on
the screen, we will use these inside of each if statement, and add (or subtract) the velo-
city variable amount for each axis (vX if we are dealing with iX, vY if we are dealing
with iY) respectively. We have initially set the vX and vY values at one, which would
equate to a relatively slow movement. If the vX and vY were set to 2, the iBagel would
 
 
Search WWH ::




Custom Search