Mouse Button Registering Too Many Times

Hi everyone,

Here are some snippets of my code:

[java] private void initKeys() {
inputManager.addMapping(“Pick”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(analogListener, new String[]{“Pick”});
}

private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float intensity, float tpf) {
  if (name.equals("Pick")) {
    // Reset results list.
    CollisionResults results = new CollisionResults();
    // Convert screen click to 3d position
    Vector2f click2d = inputManager.getCursorPosition();
    Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
    Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
    // Aim the ray from the clicked spot forwards.
    Ray ray = new Ray(click3d, dir);
    // Collect intersections between ray and all nodes in results list.
    rootNode.collideWith(ray, results);
    // (Print the results so we see what is going on:)
    for (int i = 0; i  0) {
      // The closest result is the target that the player picked:
      Geometry target = results.getClosestCollision().getGeometry();
      // Here comes the action:
      System.out.println(target.getName());
    }
  } // else if ...
}

};[/java]

So the problem I am experiencing is that every time I click, the button press is being registered more than once. In electrical engineering, the way to solve a similar problem is called debouncing. I was wondering if there is a simple way to solve this problem, as I would like to register the click just once. Thanks!

I think I figure it out. I used ActionListener instead of AnalogListener, and it works great. I guess that makes sense, as analog registers a continuous stream of events (I’m guessing that based on its name), whereas an action only happens once (until of course, it happens again or something else happens).

Here’s the code for the ActionListener, taken from the tutorials:

[java]private void initKeys() {
inputManager.addMapping(“Pick”, new MouseButtonTrigger(0));
inputManager.addListener(actionListener, “Pick”);
}

private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(“Pick”) && !keyPressed) {
// Reset results list.
CollisionResults results = new CollisionResults();
// Convert screen click to 3d position
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
// Aim the ray from the clicked spot forwards.
Ray ray = new Ray(click3d, dir);
// Collect intersections between ray and all nodes in results list.
rootNode.collideWith(ray, results);
// (Print the results so we see what is going on:)
for (int i = 0; i 0) {
// The closest result is the target that the player picked:
Geometry target = results.getClosestCollision().getGeometry();
// Here comes the action:
System.out.println(target.getName());
}
} // else if …
}
};[/java]

@funkeemonkee True dat!