This is just a minor concern at the moment, but I seem to be unable to find what's wrong with this.
The expected behaviour is that it will detect (if the mouse is within the actor's image) when a mouse button is down, save it in the list "keysPressed" so that it will only return true once per click, and return true or false depending on if this is the first act() cycle in which the mouse button was down.
The behaviour I'm getting is that, say I call "if(mouseClicked(this, "left"))", it does returns true when I press the left button, but it also returns true whenever I release the left mouse button or move the mouse around on the actor's image while the button is still held down.
Great thanks to anyone who takes a look at the code.
public boolean mouseClicked(Actor actorTemp, String button) { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse != null) { int actorXradius = actorTemp.getImage().getWidth() / 2; int actorYradius = actorTemp.getImage().getHeight() / 2; if(mouse.getX() > actorTemp.getX() - actorXradius && mouse.getX() < actorTemp.getX() + actorXradius && mouse.getY() > actorTemp.getY() - actorYradius && mouse.getY() < actorTemp.getY() + actorYradius) { if(button == "left" && mouse.getButton() == 1 && !keysPressed.contains("mouseButton1")) { keysPressed.add("mouseButton1"); return true; } if(button == "right" && mouse.getButton() == 3 && !keysPressed.contains("mouseButton3")) { keysPressed.add("mouseButton3"); return true; } if(button == "middle" && mouse.getButton() == 2 && !keysPressed.contains("mouseButton2")) { keysPressed.add("mouseButton2"); return true; } if(keysPressed.contains("mouseButton1") && mouse.getButton() != 1) { keysPressed.remove("mouseButton1"); } if(keysPressed.contains("mouseButton2") && mouse.getButton() != 2) { keysPressed.remove("mouseButton2"); } if(keysPressed.contains("mouseButton3") && mouse.getButton() != 3) { keysPressed.remove("mouseButton3"); } } } return false; }