Game Development Reference
In-Depth Information
// Start the airplane using a Timer object.
gameTimer.start();
}
The target point of the laser is stored in two fields named laserX and laserZ . Clicking or
dragging the mouse to a new location on the screen changes the value of the laserX and laserZ
fields. When the mouse is clicked or dragged, either the mouseClicked or mouseDragged method
is called.
// These methods are used to update the position of the
// laser.
public void mouseDragged(MouseEvent me) {
laserX = me.getX();
laserZ = me.getY();
}
public void mouseClicked(MouseEvent me) {
laserX = me.getX();
laserZ = me.getY();
}
The LaserSimulator class declares an inner class named GameUpdater . Inside this class is an
actionPerformed method that the Timer calls every 0.1 seconds. The first thing the method does
is to determine whether the laser is in contact with the airplane. If it is, then the value of the
elapsedTime field is incremented. If the laser loses contact with the airplane, the elapsedTime
field is reset to zero.
// This ActionListener is called by the Timer.
class GameUpdater implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Set the time increment.
double timeIncrement = 0.1;
// If the laser is hitting the airplane, increment
// the elapsedTime variable. If not, set the elapsedTime
// variable to zero.
if ( laserX > airplaneX && laserX < airplaneX + airplaneIconWidth &&
laserZ > 10 && laserZ < 10 + airplaneIconHeight ) {
elapsedTime = elapsedTime + timeIncrement;
}
else {
elapsedTime = 0.0;
}
The airplane moves horizontally across the screen at a constant speed of 20 pixels/s .
The new location of the airplane is determined and the GUI display is updated.
Search WWH ::




Custom Search