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

2019/6/9

Mouse clicked in a certain area

loldertroll loldertroll

2019/6/9

#
Is there a way to check if the mouse clicked in a certain area?
danpost danpost

2019/6/10

#
loldertroll wrote...
Is there a way to check if the mouse clicked in a certain area?
Get a MouseInfo object and compare its x and y (using the getX and getY methods) to the area extremities.
loldertroll loldertroll

2019/6/10

#
I dont really know how to use the mouseinfo stuff. Can you tell me whats wrong?
MouseInfo mouse = Greenfoot.getMouseInfo();
        {
            if(Greenfoot.getButton(1) && getX() > 773 && getX() < 110 && getY() > 42 && getY() < 125)
            {
              Greenfoot.setWorld(new world1());
            }
            
        
        }
Super_Hippo Super_Hippo

2019/6/10

#
After line 1, you need to make sure that "mouse" is not "null" before executing the rest. Furthermore, the first condition in line 3 should be "mouse.getButton() == 1" and all others should be "mouse.getX()" and "mouse.getY()" instead of "getX()" and "getY()". And you also have to swap the < and > for the X checks.
loldertroll loldertroll

2019/6/10

#
Super_Hippo wrote...
After line 1, you need to make sure that "mouse" is not "null"
how do i do that?
Super_Hippo Super_Hippo

2019/6/11

#
Put this line between lines 1 and 2:
if (mouse != null)
danpost danpost

2019/6/11

#
Super_Hippo wrote...
After line 1, you need to make sure that "mouse" is not "null"
This may not actually be needed if it is first determined that the mouse was clicked:
if (Greenfoot.mouseClicked(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse.getButton() == 1 && mouse.getX() > 110 && mouse.getX() < 773 && mouse.getY() > 42 && mouse.getY() < 125)
    {
        Greenfoot.setWorld(new world1());
    }
}
loldertroll loldertroll

2019/6/11

#
didnt work
You need to login to post a reply.