This site requires JavaScript, please enable it in your browser!
Greenfoot back
TinyTertle
TinyTertle wrote ...

2016/4/3

Mouse clicked registering twice

TinyTertle TinyTertle

2016/4/3

#
I've tested the following code and for someone reason each click counts twice (once for the press and once for the release). Can someone tell me why this is happening so I can prevent it? Thanks in advance!
MouseInfo info = Greenfoot.getMouseInfo();
        if(info != null && info.getButton() == 1){ //mouse click detected
            if(startButton.isSelected(info.getX(), info.getY())){ //start button was clicked
                startButton.execute();
            }
            else if(helpButton.isSelected(info.getX(), info.getY())){ //help button was clicked
                helpButton.execute();
            }
        }
danpost danpost

2016/4/3

#
There is no code to limit the action to one or the other (mouse button press or mouse button release). All you have is a test for a button action, so it registers on both press and release. I suggest you first determine if a click is detected, then get the mouse info, if so:
if (Greenfoot.mouseClicked(null)){
    MouseInfo info = Greenfoot.getMouseInfo();{
        if (info.getButton() == 1){
            // etc.
A test for 'info' being 'null' is not needed as it is never null when a click is detected.
TinyTertle TinyTertle

2016/4/3

#
Ahh very helpful! Thanks a bunch :D
You need to login to post a reply.