Hello there!
This is my first post on this wonderfully active forum. I'm very happy to be here, get help and hopefully help others as soon as I get a better grasp on things.
I've been programming in Java for about 1 year and started out with JMonkey a few day ago. The tutorials are superb and I'm learning quickly. I just have one problem with my code and I can't seem to figure out what I'm doing wrong.
So, I have a cube and want to be able to move it up and down (along the Y axis) with my "z" and "h"-key. I'm using a German keyboard (I'm Swiss btw), so the "z" key is "y" on US keyboards, I think. Just so you don't get confused.
Anyway, the cube moves up a LOT faster than down when pressing the respective keys…although the code doesn't say it should.
could anyone explain why this is happening?
here's the part where i set up the movement in my simpleUpdate method:
protected void simpleUpdate(){
boolean updateCube = false;
if (KeyBindingManager.getKeyBindingManager().isValidCommand("UP", true)){
moveY += .01;
updateCube = true;
if (updateCube){
box.setLocalTranslation(new Vector3f(0, moveY, 0));
}
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("DOWN", true)){
moveY -= .01;
updateCube = true;
if (updateCube){
box.setLocalTranslation(new Vector3f(0, moveY, 0));
}
}
}
and here's the whole code, should my mistake lie elsewhere:
package com.jmedemos;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
public class JME_KeyInput extends SimpleGame {
float moveY = 0f;
Box box = new Box("MyBox", new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
@Override
protected void simpleInitGame() {
box.setLocalTranslation(new Vector3f(0, 0, 0));
box.setModelBound(new BoundingBox());
box.updateModelBound();
rootNode.attachChild(box);
KeyBindingManager.getKeyBindingManager().set("UP", KeyInput.KEY_Z);
KeyBindingManager.getKeyBindingManager().set("DOWN", KeyInput.KEY_H);
}
protected void simpleUpdate(){
boolean updateCube = false;
if (KeyBindingManager.getKeyBindingManager().isValidCommand("UP", true)){
moveY += .01;
updateCube = true;
if (updateCube){
box.setLocalTranslation(new Vector3f(0, moveY, 0));
}
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("DOWN", true)){
moveY -= .01;
updateCube = true;
if (updateCube){
box.setLocalTranslation(new Vector3f(0, moveY, 0));
}
}
}
public static void main(String[] args) {
JME_KeyInput game = new JME_KeyInput();
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
game.start();
}
}
I hope you can point out what I'm doing wrong.
Thank you very much!
Jonas