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

2014/11/19

Control Interface Bug fix in a city Game please Help!

Prish950 Prish950

2014/11/19

#
I have a menu icon if i press on it, it opens the building menu and i can choose wich building i want to build. If i click on the Building the building follows the cursor and i can place it. The problem is my menu ist to big and i want that each time i have selected a building the interface closes i have a code but actually just the menu is deleting and the building don't get on my cursor but why? please Help!
public class Building1 extends Buildings
{
    int mausX;
    int mausY;
    /**
     * 
     */
    public void act() 
    {
          if(this !=null)
       {
         mausX = Greenfoot.getMouseInfo().getX();
         mausY = Greenfoot.getMouseInfo().getY();
       }
      
         if (Greenfoot.mouseClicked(this))
       {
        getWorld().addObject(new CursorBuilding1(), mausX,mausY);
         
        getWorld().addObject(new Baumenu(),50, 650);
        
        getWorld().removeObjects(getWorld().getObjects(Inteface.class));
        
         
       }
       
        
       
       
        
    }    
}
danpost danpost

2014/11/19

#
The code given will only place the building at the point of mouse-click to begin with. You need more code to maintain to building at the location of the moving mouse. Line 10 is a bit of a no-brainer. The act method is never called on 'null' objects The keyword 'this', used in any non-static method, will always refer to an object -- the object the method is being executed for or on. If the object the method was being called on was 'null', a 'NullPointerException would be thrown before the method was ever reached. Lines 12 and 13 call the 'getX' and 'getY' methods on 'Greenfoot.getMouseInfo()', which returns a MouseInfo reference that could be null. This would be an example of calling a method on a null reference which would throw a NullPointerException. You should probably make sure that Greenfoot.getMouseInfo()' does not return a null value before trying to execute those method on it.
You need to login to post a reply.