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

2012/10/10

getMouseInfo() Problem with mouse out of the screen

Mux Mux

2012/10/10

#
yeah it's already in the Headline. How to observe the mouse weather it's out of the screen or inside. I have to say I know the basics of how to use if( ) , and how to get the getX() of the mouse, but I forgot the statement that is needed inside the if( ) like: if( ?????????? ) { mouseX = Greenfoot.getMouseInfo().getX(); } I just need ???? to stop the thing to look for the X when the mouse is off screen. Like if ( Greenfoot.getMouseInfo() != null) but this one doesn't work, because of a nullPointerExeption and I don't know where the mistake is.
SPower SPower

2012/10/10

#
If you're using greenfoot 2.2 or higher, Greenfoot.getMouseInfo will return null if your mouse is out of the world. Example:
MouseInfo info = Greenfoot.getMouseInfo();
if (info != null) { // mouse in world
     mouseX = info.getX();
}
EDIT: and that exception is probably caused by something else.
Mux Mux

2012/10/10

#
Nice thats it. thx for the really fast answer
Upupzealot Upupzealot

2012/10/10

#
it seems lots of people has met this problem, luckily we have warm hearted friends like @SPower
SPower SPower

2012/10/10

#
Thanks :)
danpost danpost

2012/10/10

#
If you use
if (Greenfoot.getMouseInfo() != null)
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
the last line will always return 'null' as you already received the info in the 'if' statement and the info in Greenfoot was nullified. That is why something in the form of SPower's method needs to be used.
You need to login to post a reply.