0

I have just started using greenfoot for school and I would like to create a game like this one: http://www.scirra.com/arcade/action/455/squared but I am having trouble getting the actor/object to follow my mouse. Like the black square does in the mentioned game. I have tried this code so far but to no avail.

MouseInfo mouse = Greenfoot.getMouseInfo();
        setLocation(mouse.getX(), mouse.getY());
        if (mouse != null)
        {
        setLocation(mouse.getX(), mouse.getY());
        }
Matt
  • 75
  • 1
  • 16

2 Answers2

0

getMouseInfo

public static MouseInfo getMouseInfo() Return a mouse info object with information about the state of the mouse.

Returns: The info about the current state of the mouse, or null if the mouse cursor is outside the world boundary (unless being dragged). Basically this means your mouse if out of boundaries when that method is called.

In order to help you out more please describe what exactly where you hoping to achieve.

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30
  • I was hoping that the object would follow my mouse. This doesn't follow my mouse at all I move my mouse around in the game and the square doesn't move. I want it to follow my mouse like the black square does in the game I linked above. – Matt Feb 14 '15 at 07:22
  • i hope this will help you http://stackoverflow.com/questions/17003206/how-do-i-get-my-image-to-follow-my-mouse – Ajay Venugopal Feb 14 '15 at 07:30
  • Thanks I will try and report back. – Matt Feb 14 '15 at 07:32
  • No that didn't really help me. Sorry. :( – Matt Feb 14 '15 at 07:42
0

I found this while searching for an answer on Google and it showed how to get the object to follow your mouse. http://www.datraughber.com/prog1/greenfoot/unit3.pdf

Original Code:

    MouseInfo mouse = Greenfoot.getMouseInfo();
    setLocation(mouse.getX(), mouse.getY());

    if (mouse != null)
    {
    setLocation(mouse.getX(), mouse.getY());
    }

New Code

if(Greenfoot.mouseMoved(null))
        {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                setLocation(mouse.getX(),mouse.getY());
        }

Thanks for trying to help Ajay Venugopal

Matt
  • 75
  • 1
  • 16