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

2012/10/31

Mouse clicked on a certain area?

zhuvell zhuvell

2012/10/31

#
How can I see if mouse has clicked on a certain area (coordinates with if statement)? I don't mean clicking on actors, I mean just an area on the screen. I would also like to do this in the World class.
Gevater_Tod4711 Gevater_Tod4711

2012/10/31

#
you have to get the coordinates of the mouse to know where it's clicked. If you want a area you have to do this with coordinates like this:
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) {
    int x = mouse.getX();
    int y = mouse.getY();
}
if (Greenfoot.mouseClicked(null)) {
    if (x > minX && x < maxX && y > minY && y < maxY) {//min and max should be the edges of the area;
        //whatever you want to do if the area is clicked;
    }
}
zhuvell zhuvell

2012/10/31

#
Okay, the part that I didn't figure out was the null part, because so far I've only been using this with actors. Thank you very much for the quick reply : )
You need to login to post a reply.