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

2014/4/10

Greenfoot.getMouseInfo().getX() issues

makinetion makinetion

2014/4/10

#
Ive been using this to have an actor follow the mouse, it worked the first couple times it ran, then suddenly it stopped working and i kept getting this message java.lang.NullPointerException at Selector.placement(Selector.java:23) at Selector.act(Selector.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)
bourne bourne

2014/4/10

#
I assume the call isn't called only when a mouse event occurs, can we see the code of interest, particularly the Selector's act and placement methods
danpost danpost

2014/4/10

#
Greenfoot.getMouseInfo() may occasionally return a 'null' value. You must ensure it is not 'null' before calling 'getX()' on it:
1
2
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) setLocation(mouse.getX(), mouse.getY());
makinetion makinetion

2014/4/10

#
1
2
3
4
5
6
7
8
9
10
public void act()
   {
      placement();
      selection();
   }   
    
   public void placement()
   {
       setLocation(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
   }
makinetion makinetion

2014/4/10

#
Got it working, thanks danpost
You need to login to post a reply.