Hi all,
I've got a problem using the MouseInfo class.
Im trying to make my NumberField count up it's value if the mouse is pressed on the arrow button.
If I click the button everything is working fine but if I try to count up the time any mousebutton is pressed it doesn't work.
I want the numberfields to count up the value faster if the mouse is pressed on the object but the methods mouseDragged() (class Greenfoot), mousePressed() (class Greenfoot) and getButton() (class MouseInfo) seem to work only if the mouse button is pressed down AND the mouse is moved.
Here's the code I use:
Am I doing anything wrong or is this a bug in the Greenfoot API?
public class Arrow_Button extends Menu { private String direction; private NumberField numberField; private long time; private long startingTime; public Arrow_Button(String direction, NumberField numberField) throws IllegalArgumentException { if (direction != "up" && direction != "down") { throw new IllegalArgumentException("String direction has to be \"up\" or \"down\""); } this.direction = direction; this.numberField = numberField; setImage(new GreenfootImage("arrow_" + direction + ".jpg")); } public Arrow_Button(String direction, NumberField numberField, int width, int height) { if (direction != "up" && direction != "down") { throw new IllegalArgumentException("String direction has to be \"up\" or \"down\""); } this.direction = direction; this.numberField = numberField; setImage(new GreenfootImage("arrow_" + direction + ".jpg")); getImage().scale(width, height); } public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this) && numberField != null) { if (!numberField.isActive()) { numberField.setActiveNumberField(numberField); } if (direction == "up") { numberField.incrementValue(); } else { numberField.decrementValue(); } } if (mouse.getButton() != 0 && mouseOnObject(this) && numberField != null) { if (!numberField.isActive()) { numberField.setActiveNumberField(numberField); } if (startingTime == 0) { startingTime = System.currentTimeMillis(); } else { time = System.currentTimeMillis() - startingTime; } if (time > 500 && time % 5 == 0) { if (direction == "up") { numberField.incrementValue(); } else { numberField.decrementValue(); } } } else if (Greenfoot.mouseDragEnded(null)) { startingTime = 0; } } private boolean mouseOnObject(Actor actor) { MouseInfo mouse = Greenfoot.getMouseInfo(); return mouse != null && mouse.getX() > actor.getX() - actor.getImage().getWidth()/2 && mouse.getX() < actor.getX() + actor.getImage().getWidth()/2 && mouse.getY() > actor.getY() - actor.getImage().getHeight()/2 && mouse.getY() < actor.getY() + actor.getImage().getHeight()/2; } }