I need help with inputs

Hi, I’m new to jme and I’m stuck on user inputs.I was looking at this tutorial https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_input_system
I copied the text, fixed some errors but its not working.Whats the problem?
I’m sorry if topic is in the wrong section I’m new to the forums.

“not working” could mean so many different things. So I will pick one at random to answer:
“Immediately step away from your computer and grab a fire extinguisher. Call the fire department if you have time but get the fire out as soon as possible.”

2 Likes

Paul, you forgot the chicken waving part. That’s the 3rd most important part. Everyone forgets that part. It’s no use.

What i meant to say is: the cube wont move and rotate.

We will need to see the code. Since you’ve changed stuff from the original we cannot possibly help without it.

I didn’t change anything, I just changed the package name and class name.

@Jeckie said: I copied the text, fixed some errors...

What errors did you fix?

I don’t see anything wrong with the example posted so things should be working fine. What keys are you pressing?

We know the code of all the examples by heart, we’re not responding just to spite people.

Ok, forget what I mentioned above. I copied the code and run it. The cube won’t move, I’m pressing J,K,P and Space.Sorry if I did something wrong I don’t know why youre mad.

1 Like

They’re not mad, its just that they get this every day, vague information. Is the action listener being called at all when you press the keys?

1 Like

Nothing happens when I press the keys.Does the code work for you?

I just tried this and it works fine. A tiny bit hard to see what happens, but it works.

Hint: HOLD the key down, you’ll see the cube do its things.

If that doesn’t work, PUT THE CODE. It’s not like it’s hard you know.

@Jeckie said: Nothing happens when I press the keys.

That implies you didn’t check. Debug your code, check if the onAction method is being called by printing some text in it, to see if your keys are actually doing anything, if yes, continue to debug and find out why the cube isn’t moving. You could print its location every frame, if madjack says its hard to see the motion, printing its location will tell you exactly how much its moving by.

Ok, I’ll try it. Thank you for you help.

Also, try running the app with vsync on so that the tpf doesn’t become too small.

I find a lot of these * tpf things will run super slow if the frame rate is super high because of float rounding errors.

The cube wont move when I’m holding the key.I tried printing some text, but that doesnt work either.
Here is my code:

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;

/** Sample 5 - how to map keys and mousebuttons to actions */
public class UserInput2 extends SimpleApplication {

public static void main(String[] args) {
UserInput2 app = new UserInput2();
app.start();
}
protected Geometry player;
Boolean isRunning=true;

@Override
public void simpleInitApp() {
Box b = new Box(Vector3f.ZERO, 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
}

/** Custom Keybinding: Map named actions to inputs. */
private void initKeys() {
// You can map one or several inputs to one named action
inputManager.addMapping(“Pause”, new KeyTrigger(KeyInput.KEY_P));
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_K));
inputManager.addMapping(“Rotate”, new KeyTrigger(KeyInput.KEY_SPACE),
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
// Add the names to the action listener.
inputManager.addListener(actionListener,“Pause”);
inputManager.addListener(analogListener,“Left”, “Right”, “Rotate”);

}

private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(“Pause”) && !keyPressed) {
isRunning = !isRunning;
}
}
};

private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (isRunning) {
if (name.equals(“Rotate”)) {
player.rotate(0, valuespeed, 0);
}
if (name.equals(“Right”)) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x + value
speed, v.y, v.z);
}
if (name.equals(“Left”)) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x - value*speed, v.y, v.z);
}
} else {
System.out.println(“Press P to unpause.”);
}
}
};
}

I have copied what you pasted above, fixed the quotation marks, and it works fine here.

I don’t know what else to say here.

As an exercise I modified what you wrote. Paste it yourself. Replace that part on your side with the following. It will print the changed value.

[java]
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (isRunning) {
if (name.equals(“Rotate”)) {
float newY = value * speed;
System.out.println("The new Y rotation is now " + newY);
player.rotate(0, newY, 0);
}
if (name.equals(“Right”)) {
Vector3f v = player.getLocalTranslation();
float newX = v.x + value * speed;
System.out.println("The new X [Right] is now " + newX);
player.setLocalTranslation(newX, v.y, v.z);
}
if (name.equals(“Left”)) {
Vector3f v = player.getLocalTranslation();
float newX = v.x + value * speed;
System.out.println("The new X [Left] is now " + newX);
player.setLocalTranslation(newX * speed, v.y, v.z);
}
} else {
System.out.println(“Press P to unpause.”);
}
}
};
[/java]

I replaced the code and its still not working, it doesnt print anything to the console.
Maybe its not the problem in code, maybe its something else.Thank you for your help.

Something was wrong with my project, I made a new one and the code is working.I’m so sorry for bothering you and thank you for your help.

@Jeckie said: Something was wrong with my project, I made a new one and the code is working.I'm so sorry for bothering you and thank you for your help.

Its ok we don’t really have nothing better to do.