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

2014/10/14

MouseInfo.getButton() only works when mouse moves fix?

Entity1037 Entity1037

2014/10/14

#
The getButton() method of MouseInfo only works if the mouse is moving and it's seriously creating a problem for in scenario dragging of objects in my game. How do I make sure the object isn't let go when the mouse stops? Here's my code:
import greenfoot.*;
import java.awt.Color;

public class Mouse extends Actor
{
    Actor light=null;
    public Mouse(){
        GreenfootImage image = new GreenfootImage(1,1);
        image.setColor(Color.WHITE);
        image.fill();
        setImage(image);
    }
    public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse==null)return;
        setLocation(mouse.getX(),mouse.getY());
        if (light==null){
            light = getOneObjectAtOffset(0,0,Light.class);
        }
        if (light!=null){
            if (mouse.getButton()==1){
                light.setLocation(getX(),getY());
            }else{
                light=null;
            }
        }
    }
}   
davmac davmac

2014/10/14

#
You can call Greenfoot.mouseDragEnded() and Greenfoot.mouseClicked() to check whether the button has been released.
Entity1037 Entity1037

2014/10/14

#
Got it. Thank you!
You need to login to post a reply.