The Jumping Sprite


The Fireball

A fireball starts at the lower righthand side of the panel and travels across to the left. If it hits Jack, the fireball will explode and a corresponding sound will be heard. A fireball that has traveled off the lefthand side of the panel, or has exploded, is reused. The FireBallSprite object is repositioned somewhere on the right edge of the game panel and fired at Jack again.

Only a single fireball is on the screen at a time, so JumpingJack creates only one FireBallSprite object. It is declared in JackPanel's constructor:

     fireball = new FireBallSprite(PWIDTH, PHEIGHT, imsLoader, this, jack);

The fourth argument is a reference to JackPanel allowing the fireball to call its methods; the fifth argument is a reference to the JumperSprite object, jack, allowing the fireball to call its methods.

As the fireball moves left, it keeps checking whether it has hit Jack. If a collision occurs, JackPanel will be asked to display an explosion as FireBallSprite resets its position.

Statechart Specification

The statechart in Figure 12-22 is a useful way of specifying the design needs of FireBallSprite.

Statecharts were introduced in Chapter 11.


The update/draw cycle driven by JackPanel's animation loop is visible. There are two special cases to consider: when the fireball hits Jack and when it leaves the left side of the panel.

Figure 12-22. The FireBallSprite statechart


The examining environment and move states are represented by updateSprite( ):

     public void updateSprite( )     { hasHitJack( );       goneOffScreen( );       super.updateSprite( );     }     private void hasHitJack( )     /* If the ball has hit jack, tell JackPanel (which will        display an explosion and play a clip), and begin again.     */     { Rectangle jackBox = jack.getMyRectangle( );       jackBox.grow(-jackBox.width/3, 0);   // make bounding box thinner       if (jackBox.intersects( getMyRectangle( ) )) {    // collision?         jp.showExplosion(locx, locy+getHeight( )/2);                // tell JackPanel, supplying it with a hit coordinate         initPosition( );       }     } // end of hasHitJack( )     private void goneOffScreen( )     {       if (((locx+getWidth( )) <= 0) && (dx < 0)) // gone off left         initPosition( );   // start the ball in a new position     }

Collision detection (the [has hit jack] condition in the statechart) is carried out by obtaining Jack's bounding box and checking if it intersects the bounding box for the fireball. The bounding box dimensions for Jack are temporarily reduced a little to trigger a collision only when the fireball is right on top of him.

The move state is dealt with by Sprite's updateSprite( ), which is called from FireBallSprite's updateSprite( ). The draw state is implemented by Sprite's drawSprite( ) method.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net