Hi I’m new to jMonkeyEngine. I would like to achieve an event triggered only once during button being pressed and lifted if possible. This is because I’d like to move the “player” stepwise.
I have mostly followed the getting started example and the docs on input handling.
I am confused since by default, even though I only map WASD, other buttons and the mouse affects the window. This is highly annoying, how do I get rid of this?
(BTW I start the ClientWindow from my main method elsewhere.)
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
public class ClientWindow extends SimpleApplication {
private boolean isRunning = true;
protected Geometry player;
@Override
public void simpleInitApp() {
Box b = new Box(1, 1, 1);
player = new Geometry("Player", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
player.setMaterial(mat);
rootNode.attachChild(player);
initKeys(); // load my custom keybinding
}
private void initKeys() {
// You can map one or several inputs to one named action
//inputManager.deleteMapping( SimpleApplication.INPUT_MAPPING_MEMORY );
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W),
new KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A),
new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D),
new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S),
new KeyTrigger(KeyInput.KEY_DOWN));
// Add the names to the action listener.
inputManager.addListener(actionListener, "Up", "Left", "Right", "Down");
// inputManager.addListener(analogListener, "Up", "Left", "Right", "Down");
}
private final ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String name, boolean keyPressed, float tfp) {
if(isRunning){
float value=0.4f;//arbitrary offset for now.
if (name.equals("Up") && !keyPressed) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x, v.y + value /** speed*/, v.z);
}
if (name.equals("Down")&& !keyPressed) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x, v.y - value /** speed*/, v.z);
}
if (name.equals("Right")&& !keyPressed) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x - value /** speed*/, v.y, v.z);
}
if (name.equals("Left")&& !keyPressed) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x + value /** speed*/, v.y, v.z);
}
}
}
};
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
}