|
How to create a custom behavior class |
|
|
The Behavior class is an abstract class that provides the mechanism to include
code to change the scene graph. The Behavior class, and its descendants, are
links to user code providing changes to the graphics and sounds of the virtual
universe.
The recipe for writing custom behavior classes is listed here:
- write (at least one) constructor
store a reference to the object of change
- override public void initialization()
specify initial wakeup criteria (trigger)
- override public void processStimulus()
decode the trigger condition
act according to the trigger condition
reset trigger as appropriate
To illustrate the recipe a program is written here.the program will implement a
simple behavior of making something rotate in response to a keyboard key press.
public class SimpleBehavior extends Behavior {
private TransformGroup targetTG;
private Transform3D rotation = new Transform3D();
private double angle = 0.0;
public SimpleBehavior(TransformGroup targetTG) {
this.targetTG = targetTG;
}
/**
* initialize the Behavior
*
* set initial wakeup condition called when behavior
* beacomes live
*/
public void initialize() {
// set initial wakeup condition
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
/**
* behave
*
* called by Java 3D when appropriate stimulus occures
*/
public void processStimulus(Enumeration criteria) {
// decode event
// do what is necessary
angle += 0.1;
rotation.rotY(angle);
targetTG.setTransform(rotation);
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.