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

2017/4/5

Hover Detection?

Gaming_100 Gaming_100

2017/4/5

#
How do i detect when a mouse is hovering over a button? I want to know because i want on my start screen that when I'm hovering over a button it gets bigger. Best Regards - Gaming_100
Super_Hippo Super_Hippo

2017/4/5

#
Try out 'Greenfoot.mouseMoved(this)'. You will probably need a boolean to track the state and change it when the mouse is moving to and from the object. Because if you just use the method as the condition to use the big image and if not, then the small one, it would only be big as long as you move the mouse.
Nosson1459 Nosson1459

2017/4/5

#
You can use the following class:
import greenfoot.Actor;
import greenfoot.Greenfoot;

/**
 * Write a description of class Buttons here.
 *
 * @author Yehuda (1/2 of Nosson1459 - greenfoot.org user name)
 * @version (a version number or a date)
 */
public abstract class Buttons extends Actor {

    private Actor actorHoveredOver = null;

    /**
     * This method checks to see which button the mouse is hovering over
     */
    private void hoverOwner() {
        if ((actorHoveredOver == null || actorHoveredOver.getWorld() == null)
                && Greenfoot.mouseMoved(this)) {
            actorHoveredOver = this;
        } else if (actorHoveredOver == this && Greenfoot.mouseMoved(null)
                && !Greenfoot.mouseMoved(this)) {
            actorHoveredOver = null;
        }
    }

    /**
     * This methods returns true if the mouse is hovering over the specified
     * button.
     *
     * @param button the button to see if hovering over
     * @return true, if mouse is over specified button
     */
    public boolean mouseHoveringOver(Actor actor) {
        hoverOwner();
        return actorHoveredOver == actor;
    }

    /**
     * Gets the button that the mouse is hovering over.
     *
     * @return the button that the mouse is over
     */
    public Actor getHoverOwner() {
        return actorHoveredOver;
    }
}
Now you can extend your button(s) from this class and to make your image bigger you can do:
        if (mouseHoveringOver(this)) {
            setImage(/*bigger image*/);
        } else {
            setImage(/*regular image*/);
        }
danpost danpost

2017/4/6

#
The following would be sufficient in your button class act method:
if (Greenfoot.mouseMoved(this))
    setImage(/**bigger image*/);
if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
    setImage(/**regular image*/);
The button is only interested in whether the mouse is over itself and does not need to know about any other actor that might currently own the hover. As a sidenote, be aware that you should not continuously scale the image back and forth for sizing it as the more you scale it, the more detail in the image is lost. Either keep a reference to both images used (you can scale once for one of them) or have both image in files in your 'images' folder of the project. The first way is the preferred way as it only needs to set a reference to the image and not have to read the file and create the image, which would be done every time the mouse moved.
Gaming_100 Gaming_100

2017/4/7

#
@danpost Thanks very much! IT WORKS!! Admire your work a lot! Thank you very much! :) :D
You need to login to post a reply.