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

2020/2/9

Change Background while Mouse ist over a specific area

xandreas_1 xandreas_1

2020/2/9

#
Hi guys, I want to create a code, that changes the background when I am with the mouth over a specific area of the background. Do you know how to do that?
danpost danpost

2020/2/9

#
Use a boolean field to indicate mouse being over that area or not (or indicating background state):
// field
private boolean altBackground;

// via act
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null && (mouse.getX > 100 && mouse.getX() < 200 && mouse.getY() > 300 && mouse.getY() < 400) != altBackground)
{
    altBackground = ! altBackground;
    setBackground(altBackground ? "altBackground.jpg" : "mainBackground.jpg");
}
You need to login to post a reply.