1

I don't quite get how this bit of code works. I understand the outcome (to add a new object using the mouse coordinates), but can you explain to me how the lines beginning with 'MouseInfo...' and 'addObject..' work? Is a new MouseInfo object created for each click event called 'mouse'?

public void act() 
{
    // Add your action code here.
    if( Greenfoot.mouseClicked(this)) {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        addObject( new Frog(), mouse.getX(), mouse.getY());
    }
}  

1 Answers1

0

When you invoke Greenfoot.getMouseInfo() you receive a MouseInfo object from the Greenfoot class. It could be a new Object for every click or one that gets reused and updated as the user clicks.

The MouseInfo object has the methods getX() and getY() which return the coordinates of the click. Finally the addObject method adds a new frog at the specified position.

mercutio
  • 1,065
  • 7
  • 18