Game Development Reference
In-Depth Information
move twice as fast (it would move by two pixels on each pulse event, instead of by one
pixel).
If the right Boolean variable is true, we want your iBagel object to move in the
positive direction along the X axis, so we would use an if(right){iX+=vX} pro-
gramming statement to add the vX velocity value to the iX location value using the +=
operator we learned about in Chapter 3 . Similarly, if the left Boolean variable is true,
we would use an if(left){iX-=vX} programming statement, which will subtract
the vX velocity value from the iX location value, using the -= Java operator.
We will do essentially the same thing along the Y axis when the up and down (or
W and S) keys are pressed. If the down Boolean variable is true, we want the iBagel
object to move in the positive direction along the Y axis. Thus we would use an
if(down){iY+=vY} programming statement, which will add the vY velocity value
to the iY location value, using the += operator. In JavaFX, a positive X value goes
from the 0,0 origin to the right, while positive Y values go from 0,0 down. Finally, to
move the iBagel up, we will use an if(up){iY-=vY} programming statement,
which will subtract the vY velocity value from the iY location value, using the -= oper-
ator. The basic Java code to perform these four conditional if statement evaluations,
and their respective X or Y sprite movement calculations inside of the Bagel class .up-
date() method, is shown in Figure 11-13 , and should look like the following method
body structure thus far:
@Override
public void update() {
if( right ) { iX += vX }
if( left ) { iX -= vX }
if( down ) { iY += vY }
if( up )
{ iY -= vY }
}
 
Search WWH ::




Custom Search