This is my code:
.
.
inputManager.addMapping(“Touch”, new TouchTrigger(0));
inputManager.addListener(simpleTapListener, “Touch”);
And the listener:
[java]
private TouchListener simpleTapListener = new TouchListener() {
public void onTouch(String name, TouchEvent event, float tpf) {
/String eventType=""+event.getType();
if(!preTest && !(eventType.equals(“SCROLL”) && prev_event.equals(“SCROLL”)) && !(eventType.equals(“SCALE”) && prev_event.equals(“SCALE”)) )
happenedEvents.add(""+eventType);
prev_event=eventType;/
int prev_ocurrencias = eventosOcurrencias.get(event.getType());
eventosOcurrencias.put(event.getType(), prev_ocurrencias+1);
if(!interact){
interact= true;
timeFirstInteraction = System.currentTimeMillis() - initTime;
firstInteract=true;
}
//System.out.println(firstInteract);
//System.out.println(event.getType());
switch (event.getType()) {
case TAP:
CollisionResults results2 = new CollisionResults();
// Convert screen click to 3d position
Vector2f click2d2 = inputManager.getCursorPosition();
Vector3f click3d2 = cam.getWorldCoordinates(new Vector2f(click2d2.x, click2d2.y), 0f).clone();
Vector3f dir2 = cam.getWorldCoordinates(new Vector2f(click2d2.x, click2d2.y), 1f).subtractLocal(click3d2).negate();
dir2.y = -dir2.y;
Ray ray2 = new Ray(click3d2, dir2);
// Collect intersections between ray and all nodes in results list.
rootNode.collideWith(ray2, results2);
if (results2.size() > 1) {
// The closest result is the target that the player picked:
Geometry target2 = results2.getClosestCollision().getGeometry();
// Here comes the action:
if (!simpleTapDone && target2.equals(object_geo1)) {
simpleTapDone = true;
//Material m = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//m.setColor("Color", ColorRGBA.Green);
if(!preTest){
finalTime = System.currentTimeMillis();
//addNextLabel();
tests.add(new Test(TestOptions.SIMPLE_TAP, finalTime - initTime, ""+id_sujeto, eventosOcurrencias,timeFirstInteraction));
System.out.println("New test : Tab, " + (finalTime - initTime)+"FIRST ="+timeFirstInteraction);
// makeSimpleTapTest = true;
setText("Correcto!",0);
initTime=0;
}
else
setText("Correcto!",0);
starsExplosion();
playAudio();
}
else if(preTest){
if (target2.equals(retry_geo)) {
//setText("PreTest - Tab",0);
options = new TestOptions(TestOptions.SIMPLE_TAP);
simpleTapDone = false;
initSimpleTapTest();
}
else if(target2.equals(init_experiment_geo)){
try{
rootNode.detachChild(retry_geo);
rootNode.detachChild(init_experiment_geo);
}catch(Exception e){
;
}
makeSimpleTapTest=true;
preTest=false;
}
}
else if(target2.equals(next_geo)){
if(simpleTapDone){
stopAudio();
}
else{
initTime = 0;
setText("No lo has conseguido!",0);
tests.add(new Test(TestOptions.SIMPLE_TAP, -1,""+id_sujeto,eventosOcurrencias,timeFirstInteraction));
initTime=0;
System.out.println("New test : SimpleTap, -1");
}
makeSimpleTapTest = true;
}
}
break;
case DOWN:
if(firstInteract){
CollisionResults results3 = new CollisionResults();
// Convert screen click to 3d position
Vector2f click2d3 = inputManager.getCursorPosition();
Vector3f click3d3 = cam.getWorldCoordinates(new Vector2f(click2d3.x, click2d3.y), 0f).clone();
Vector3f dir3 = cam.getWorldCoordinates(new Vector2f(click2d3.x, click2d3.y), 1f).subtractLocal(click3d3).negate();
dir3.y = -dir3.y;
Ray ray3 = new Ray(click3d3, dir3);
// Collect intersections between ray and all nodes in results list.
rootNode.collideWith(ray3, results3);
if (results3.size() > 1) {
// The closest result is the target that the player picked:
Geometry target3 = results3.getClosestCollision().getGeometry();
// Here comes the action:
if (target3.equals(next_geo))
timeFirstInteraction=-1;
}
}
break;
case LONGPRESSED:
playNegativeFeedback();
break;
case UP:
if(!simpleTapDone){
CollisionResults results3 = new CollisionResults();
// Convert screen click to 3d position
Vector2f click2d3 = inputManager.getCursorPosition();
Vector3f click3d3 = cam.getWorldCoordinates(new Vector2f(click2d3.x, click2d3.y), 0f).clone();
Vector3f dir3 = cam.getWorldCoordinates(new Vector2f(click2d3.x, click2d3.y), 1f).subtractLocal(click3d3).negate();
dir3.y = -dir3.y;
Ray ray3 = new Ray(click3d3, dir3);
// Collect intersections between ray and all nodes in results list.
rootNode.collideWith(ray3, results3);
if (results3.size() > 1) {
// The closest result is the target that the player picked:
Geometry target3 = results3.getClosestCollision().getGeometry();
// Here comes the action:
if (!target3.equals(next_geo)&&!target3.equals(object_geo1)&&!target3.equals(init_experiment_geo)&&!target3.equals(retry_geo))
playNegativeFeedback();
}
}
break;
}
firstInteract= false;
event.setConsumed();
}
};
[/java]