- jMonkeyEngine 3.0 Beginner’s Guide
- Ruth Kusterer
- 257字
- 2025-04-04 22:38:53
Time for action – trigger meets mapping
The SimpleApplication
class provides you with a handy inputManager
object that you can configure.
- Go to the
simpleInitApp()
method. Leave the template code that creates the blue cube as it is. - Register your mappings and triggers with the
inputManager
. At the beginning of the method, add the following two lines:public void simpleInitApp(){ inputManager.addMapping(MAPPING_COLOR, TRIGGER_COLOR); inputManager.addMapping(MAPPING_ROTATE, TRIGGER_ROTATE); // … }
- But what if half of your users think the Space bar is unintuitive for toggling color, and prefer the C key instead? You can easily allow several variants in one mapping; define a trigger object for the C key on the class level, as you did for the Space bar key:
private final static Trigger TRIGGER_COLOR2 = new KeyTrigger(KeyInput.KEY_C);
- Register the alternative trigger to the mapping, with a comma as separator. Change the existing
MAPPING_COLOR
line as follows:inputManager.addMapping(MAPPING_COLOR, TRIGGER_COLOR, TRIGGER_COLOR2);
You now have defined your input mappings.
What just happened?
The inputManager
object uses mappings to associate action names and triggers. You use mappings because the implementation of the action may change, and the assignment of keys or mouse clicks may change—but the mapping always remains the same. You can map one unique action name, such as MAPPING_COLOR
to several triggers, such as TRIGGER_COLOR
/Space bar and TRIGGER_COLOR2
/C key. You can use each trigger only once (in one mapping) at the same time.
Now you have triggers, action names, and input mappings, but they don't do anything yet. Your mapping needs to be registered to the appropriate listener.