Controlling the Touring Sprite


Behaviors in Java 3D

A Behavior object is used to monitor events occurring in a Java 3D application, such as key presses, the rendering of frames, the passage of time, the movement of the user's viewpoint, TRansform3D changes, and collisions. These events, called wakeup criteria, activate the Behavior object so it can carry out specified tasks.

A typical Behavior subclass has the following format:

     public class FooBehavior extends Behavior     {       private WakeupCondition wc;   // what will wake the object       // other global variables       public FooBehavior(...)       {  // initialise globals           wc = new ...  //  create the wakeup criteria       }       public void initialize( )       // register interest in the wakeup criteria       {  wakeupOn(wc);  }       public void processStimulus(Enumeration criteria)       {          WakeupCriterion wakeup;          while (criteria.hasMoreElements( ) ) {            wakeup = (WakeupCriterion) criteria.nextElement( );            // determine the type of criterion assigned to wakeup;            // carry out the relevant task;          }          wakeupOn(wc);  // reregister interest        } // end of processStimulus( )     }  // end of FooBehavior class

A subclass of Behavior must implement initialize( ) and processStimulus( ). initialize( ) should register the behavior's wakeup criteria, but other initialization code can be placed in the constructor for the class. processStimulus( ) is called by Java 3D when an event (or events) of interest to the behavior is received. Often, processStimulus( ) being called is enough to decide what task should be carried out, e.g., TimeBehavior. In more complex classes, the events passed to the object must be analyzed. For example, a key press may be the wakeup condition, but the code will need to determine which key was pressed.

A common error when implementing processStimulus( ) is to forget to re-register the wakeup criteria at the end of the method:

                 wakeupOn(wc);  // reregister interest

If this is skipped, the behavior won't be triggered again.


A WakeupCondition object can be a combination of one or more WakeupCriterion. There are many subclasses of WakeupCriterion, including:


WakeupOnAWTEvent

For AWT events such as key presses and mouse movements. WakeupOnAWTEvent is used in TouristControls.


WakeupOnElapsedFrames

An event can be generated after a specified number of renderings. This criterion should be used with care since it may result in the object being triggered many times per second.


WakeupOnElapsedTime

An event can be generated after a specified time interval. WakeupOnElapsedTime is used in TimeBehavior.

Another common mistake when using Behaviors is to forget to specify a scheduling volume (or region) with setSchedulingBounds( ). A Behavior node is only active (and able to receive events) when the user's viewpoint intersects a Behavior object's scheduling volume. If no volume is set, then the Behavior will never be triggered.



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