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

Moving the Viewpoint

FPShooter3D moves the viewpoint by attaching a KeyBehavior object to ViewPlatform's TransformGroup:

    ViewingPlatform vp = su.getViewingPlatform();
   
    KeyBehavior keyBeh = new KeyBehavior( ammoMan );
           // keyBeh can ask the ammoManager to fire a beam
    keyBeh.setSchedulingBounds(bounds);
    vp.setViewPlatformBehavior(keyBeh);

KeyBehavior's internals are similar to earlier keyboard-based behaviors developed for the Tour3D and AnimTour3D examples in Chapters 18 and 19. A key press triggers the behavior, and the associated KeyEvent object is used to determine the type of movement to carry out.

Similarities aside though, one difference between KeyBehavior and earlier classes is that this class extends ViewPlatformBehavior so it can work upon ViewPlatform's TransformGroup, called targetTG. targetTG is available to the methods defined in KeyBehavior through inheritance.

Another difference is that the Tour3D and AnimTour3D behaviors pass the responsibility for movement and rotation to objects in the scene (i.e., to the sprites). KeyBehavior carries out those operations for itself, by manipulating targetTG. KeyBehavior detects key presses of the arrow keys, optionally combined with the alt key to move forward, back, left, right, up, down, and rotate around the y-axis. The f key requests beam firing.

processStimulus() calls processKeyEvent() when a key is detected:

 private void processKeyEvent(KeyEvent eventKey) { int keyCode = eventKey.getKeyCode(); if(eventKey.isAltDown()) // ...
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