I have re-written a code from here. to get the touch location( x, y).
Here is my source code.
[java]import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.input.event.TouchEvent;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.input.controls.TouchListener;
public class Main extends SimpleApplication implements TouchListener
{
float posX=0, posY=0;
BitmapText helloText;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box”, b);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
guiNode.detachAllChildren();
guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);
helloText = new BitmapText(guiFont, false);
helloText.setSize(guiFont.getCharSet().getRenderedSize());
helloText.setText(“Touch on Display”);
helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
guiNode.attachChild(helloText);
}
@Override
public void simpleUpdate(float tpf) {
guiNode.detachAllChildren();
helloText.setText( posX + " , " + posY);
guiNode.attachChild(helloText);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
public void onTouch(String name, TouchEvent event, float tpf) {
posX = event.getX();
// throw new UnsupportedOperationException(“Not supported yet.”);
}
}[/java]
Am i written the right code to get touch location ?
You have to register a listener with the inputManager in order to get callbacks for any input event.
In simpleInitApp, put in something like:
[java]
inputManager.addMapping(“Touch”, new TouchTrigger(TouchInput.ALL));
inputManager.addListener(this, new String[]{“Touch”});
[/java]
You probably should review the Hello Input tutorial.
1 Like
May I ask that TouchInput.ALL means all operations except for specific buttons(KEYCODE_BACK)?