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

2022/10/11

Greenfoot don't detect the mouse

RobinGG RobinGG

2022/10/11

#
Hello, I have a problem. I make a code that changes the color of a GreenfootImage if the mouse goes over it but the code doesn't seem to detect my mouse no matter what. I can't figure out what's wrong.
public void act(){
        if((mouse != null) && (!(Greenfoot.mouseMoved(null)))){
            if(((mouse.getX() > 100) && (mouse.getX() < 200)) && ((mouse.getY() > 300) && (mouse.getY() < 350))){
                BA_cursus.clear();
                getBackground().drawImage(BA_cursus_C, 100, 300);
            }else{
                BA_cursus_C.clear();
                getBackground().drawImage(BA_cursus, 100, 300);
            }
        }
    }
danpost danpost

2022/10/11

#
RobinGG wrote...
I make a code that changes the color of a GreenfootImage if the mouse goes over it but the code doesn't seem to detect my mouse no matter what. << Code Omitted >>
How is the variable mouse defined? If set by something like this:
private MouseInfo mouse = Greenfoot.getMouseInfo();
then it will have the mouse state at the time the object of the class was created. Its state does not update automatically with time. You need to get a new MouseInfo object each time you want to acquire updated information about the mouse. That being said, having an instance field for the mouse does not make any real sense. Remove the instance field and add the following as first line in act:
MouseInfo mouse = Greenfoot.getMouseInfo();
You need to login to post a reply.