Game Development Reference
In-Depth Information
Shooting Projectiles: Coding the .shootProjectile()
Method
If you have not already created an empty private void shootPro-
jectile(){} method structure to get rid of those wavy red error highlights in your
code, you can do so now. Inside of this method, we will again have the if(!takeSides)
and if(takeSides) conditional structures, to segregate the different logic for each side of
the Stage. This is similar to what we did to animate the Enemy character onto the
screen in the .initiateAttack() method. The first Java statement will position the Y loca-
tion, this time using the randomOffset variable inside of the .setTranslateY() method
call off of an iBullet.spriteFrame ImageView object. The randomOffset variable adjusts
bullet placement relative to the bazooka barrel. The next two .setScaleX and
.setScaleY() method calls reduce the bullet image scale by half (0.5) and also flip the
bullet using the -0.5 value. It is interesting to note here than any negative value, not just
-1, will mirror around an axis. The next line of code sets the bulletRange variable to
-50 , before the if(bulletOffset >= bulletRange) conditional statement animates the
bullet into position using a high velocity four pixels per pulse setting. This is coded the
same way that we did this for the Enemy sprite, by using the bulletOffset variable that
is used in the counter logic inside of the .setTranslateX() method call off of the iBul-
let.spriteFrame ImageView object inside of the if portion of the conditional statement.
The else part of the if-else statement sets the shootProjectile variable to false , so only
one shot is fired!
The Java code for the if(takeSides) conditional if structure is similar, except it uses
+=4 , a bulletRange of 624 , and an if(bulletOffset <= bulletRange) evaluation state-
ment. Your Java code for these if(!takeSides) and if(takeSides) structures inside of the
shootProjectile() method body can be seen in Figure 17-43 , and should look like the
following:
private void shootProjectile() {
if( !takeSides ) {
invinciBagel.iBullet.spriteFrame.setTranslateY( randomOffset );
invinciBagel.iBullet.spriteFrame.setScaleX( -0.5 );
invinciBagel.iBullet.spriteFrame.setScaleY( 0.5 );
bulletRange = -50 ;
if( bulletOffset >= bulletRange ) {
bulletOffset-=4 ;
invinciBagel.iBullet.spriteFrame.setTranslateX( bulletOffset );
 
Search WWH ::




Custom Search