Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

Firing the Beam

The FireBeam thread is started from updateScene() like so:

    new FireBeam(intercept, this, laser, explsClip, turnAngle).start();

A basic question is why use a thread? One answer is that by passing the job of beam delivery and explosion to a new thread, the ShootingBehaviour object is free to do other tasks. For example, it could show a puff of animated smoke rising from the gun's cone or have the cone recoil slightly. Shooter3D doesn't do these things, as that would further complicate the example. At present, the threaded implementation allows updateScene() to process a user's new pick selection once the finishedShot Boolean has been set to true near the end of run() in the FireBeam thread:

    public void run()
    {
      laser.shootBeam(intercept);
      shooter.setFinishedShot();    // beam has reached its target
      explsClip.showExplosion(turnAngle, intercept);   // boom!
    }

The call to setFinishedShot() sets finishedShot to true, which permits updateScene() to respond to user clicks and, simultaneously, the explosion for the current beam will be initiated from FireBeam. This improves the responsiveness of the application since the explosion animation lasts one to two seconds.

However, there's a problem: what if the explosion animation for the beam (i.e., the current call to showExplosion()) doesn't finish before the FireBeam thread for the next beam calls showExplosion() again? The worst that happens is an interruption to the explosion animation and the truncation of the playing of the sound. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page