Controlling Animations in an AppState

Hi Forum,

I’ve developed a character in blender with Walk, Jump and Slide animations that I can load in jmonkey and assign triggers to, which work perfectly. Everything’s been fine so far but as with any game, I need to have app states for each game state be it the game screen, starting screen or option screen. So far I know how to make an App state for each of the stated game states and I’ve created a separate class for each appstate, however I ran into a problem when writing the code for my GameScreenState. See my problem is that in my GameScreenState (which is the appstate with the actual game screen with the characters and HUD etc…) , What I’m trying to do in this appstate is I’m trying to load the character and background and be able to control the character in this appstate. So far I’ve loaded the character and the background however I don’t know how to assign triggers to be able to control his movements. Please feel free to ask any questions and input would be greatly appreciated :slight_smile:

Thank you in advance
Dan

Hi Dan.

This is the same as in a simple-state application;
You simply have to add your inputManager entries in the setEnable(true) , and remove them is the setEnable(false).

Then, when you activate this appState, your controls are activated; when you deactivate the state, so do the controls.

@yang71 said: Hi Dan.

This is the same as in a simple-state application;
You simply have to add your inputManager entries in the setEnable(true) , and remove them is the setEnable(false).

Then, when you activate this appState, your controls are activated; when you deactivate the state, so do the controls.

Hi :) Thank you for responding so quickly but could you please explain further. And if it would help, I'm willing to post my code if you need me to so that you could please point out specifically where I would have to add my inputManager entries. Much appreciated :)

Thank you
Dan

Here is an example from one of my projects :
[java]
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (enabled) {
inputManager.addListener(this, “shootC”, “shootCC”,
“leftC”, “rightC”, “upC”, “downC”);
} else {
inputManager.removeListener(this);
}
}
[/java]

And of course you also need :
[java]
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// whatever you have to initialize
inputManager.addMapping(“leftC”, new KeyTrigger(KeyInput.KEY_NUMPAD4));
inputManager.addMapping(“rightC”, new KeyTrigger(KeyInput.KEY_NUMPAD6));
inputManager.addMapping(“upC”, new KeyTrigger(KeyInput.KEY_NUMPAD8));
inputManager.addMapping(“downC”, new KeyTrigger(KeyInput.KEY_NUMPAD2));
inputManager.addMapping(“shootC”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping(“shootCC”, new KeyTrigger(KeyInput.KEY_RETURN));
}
[/java]

2 Likes
@yang71 said: Here is an example from one of my projects : [java] @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); if (enabled) { inputManager.addListener(this, "shootC", "shootCC", "leftC", "rightC", "upC", "downC"); } else { inputManager.removeListener(this); } } [/java]

And of course you also need :
[java]
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// whatever you have to initialize
inputManager.addMapping(“leftC”, new KeyTrigger(KeyInput.KEY_NUMPAD4));
inputManager.addMapping(“rightC”, new KeyTrigger(KeyInput.KEY_NUMPAD6));
inputManager.addMapping(“upC”, new KeyTrigger(KeyInput.KEY_NUMPAD8));
inputManager.addMapping(“downC”, new KeyTrigger(KeyInput.KEY_NUMPAD2));
inputManager.addMapping(“shootC”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping(“shootCC”, new KeyTrigger(KeyInput.KEY_RETURN));
}
[/java]


Hi again,
Just tried this out and I’m still having the same problem where inputManager becomes underlined and it says that “cannot find symbol” and I did everything you said. Would it help if I told you that I had an analogListener ?
Thanks again :slight_smile:
Dan

For sure… I simply followed the advice of the tutorial :
[java]
private AssetManager assetManager;
private InputManager inputManager;
private AppStateManager stateManager;
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
assetManager = app.getAssetManager();
inputManager = app.getInputManager();
this.stateManager = stateManager;
// …
[/java]

This is basic OO programming/Java. The inputManager is a field of the SimpleApplication class. You cannot expect it to be visible from another class unless you pass in either the input manager or the simple application and access it that way…