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

2020/1/13

moving with the mouse

Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/13

#
I'm making a game where the hero moves by following the mouse but I can't figure it out.
danpost danpost

2020/1/13

#
Bigbobatesloppyjoe wrote...
I'm making a game where the hero moves by following the mouse but I can't figure it out.
What did you try?
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/13

#
um well I don't really know how the mouse in Greenfoot works so I looked at the documentation and some video's but I don't understand them. so let me rephrase I've been looking on how to use the mouse but I can't find anything that helps.
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/13

#
here let me restart. is there a mouse x and y in greenfoot?
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/13

#
or a method like it and if so how do i use it?
danpost danpost

2020/1/13

#
Bigbobatesloppyjoe wrote...
here let me restart. is there a mouse x and y in greenfoot?
Yes. Please refer to the MouseInfo class API.
Bigbobatesloppyjoe wrote...
or a method like it and if so how do i use it?
The Greenfoot class has a method to get a current MouseInfo object, if one exists.
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/14

#
danpost wrote...
The Greenfoot class has a method to get a current MouseInfo object, if one exists.
I found the getMouseInfo.
public void act() 
    {
      Greenfoot.getMouseInfo();
      
    } 
and now I'm a bit lost on how to use it.
danpost danpost

2020/1/14

#
Bigbobatesloppyjoe wrote...
I found the getMouseInfo. << Code Omitted >> and now I'm a bit lost on how to use it.
Well, you need to hold it in a variable so you can apply methods to it.
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/14

#
okay.
 public void act() 
    {
      MouseInfo mouseInfo = Greenfoot.getMouseInfo();
      int x = mouseInfo.getX();
      int y = mouseInfo.getY();
       setLocation(x,y);
      
    } 
but im getting an error message for line 4, java.lang.NullPointerException.
danpost danpost

2020/1/14

#
Bigbobatesloppyjoe wrote...
okay. << Code Omitted >> but im getting an error message for line 4, java.lang.NullPointerException.
I said:
The Greenfoot class has a method to get a current MouseInfo object, if one exists.
You need to make sure one exists:
if (mouseInfo != null)
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/15

#
where should I put this code?
Bigbobatesloppyjoe Bigbobatesloppyjoe

2020/1/15

#
never mind I figured it out thanks for helping.
You need to login to post a reply.