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

2014/1/3

how to use mouse's location

deathburrito16 deathburrito16

2014/1/3

#
sorry I don't know this, but how do you use getX() in the class mouseinfo?
deathburrito16 deathburrito16

2014/1/3

#
I tried MouseInfo.getX() but that didn't work.
deathburrito16 deathburrito16

2014/1/3

#
somebody help me!
deathburrito16 deathburrito16

2014/1/3

#
figured this out, but greenfoot terminal window popped up and said this. java.lang.NullPointerException at Redicle.act(Redicle.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/1/3

#
Unless a mouse action is detected internally (the mouse is moved or a button state is changed), no MouseInfo object will be returned by 'Greenfoot.getMouseInfo()' (the returned value is 'null'). Check that the object is not 'null' before calling any MouseInfo methods on the object.
deathburrito16 deathburrito16

2014/1/3

#
so I would say:
1
2
if (Greenfoot.getMouseInfo != null)
{turnTowards((Greenfoot.getMouseInfo().getX()), (Greenfoot.getMouseInfo().getY()));}
right?
danpost danpost

2014/1/3

#
I would avoid using 'getMouseInfo' so many times and only 'turnTowards' the mouse if the mouse has moved:
1
2
3
4
5
if (Greenfoot.mouseMoved(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    turnTowards(mouse.getX(), mouse.getY());
}
You need to login to post a reply.