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

2013/5/10

Error in code

geekykid2013 geekykid2013

2013/5/10

#
I have created a ball to intersect with some objects on my interface when it bounces off the paddle (as in the breakout game) however when I compile by Actor I get an error message saying cannot find symbol - variable goldEgg. what can I do?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The Robot of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author  
 * @version (Version 1.0)
 */
public class Robot extends Actor
{
   private int deltaX; //  delat is movement in the x direction
   private int deltaY; //  delta is movement in the y direction
   private int count = 2;
   
   private boolean fixed = true; // ????
   
   public Robot()
   {
   }
   
   /**
    * Act. Move the Robot if we're not fixed to paddle.
    */
   public void act()
   {
       if (!fixed)
       {
           move();
           makeSmoke();
           checkOut();
           checkEggs();
       }
   }
   
   /**
    * Move the Robot. Thencheck what we've hit.
    */
   public void move()
   {
       setLocation (getX() + deltaX, getY() + deltaY);
       checkPaddle();
       checkWalls();
   }
   
   /**
    * Check whether we've hit one of the three walls. Reverse direction
    */
   private void checkWalls()
   {
       if (getX() == 0 || getX() == getWorld().getWidth()-1) {
        deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
   }
   
   /**
    * Check whether we're out (bottom of screen).
    */
   private void checkOut()
   {
       if (getY() == getWorld().getHeight()-1) {
        ((SquareWorld) getWorld()).RobotIsOut();
        getWorld().removeObject(this);
        }
   }
   
   
   /**
    * Check whether we have hit an egg, and make the egg disappear if we have
    */
   
   private void checkEggs()
   {
       Actor egg = getOneIntersectingObject(goldEgg.class);
       
       if (goldEgg != null)
       {
           getWorld().removeObject(goldEgg);
           deltaY = -deltaY;
       }
    }
   
   private void checkPaddle()
   {
       Actor paddle = getOneIntersectingObject(Paddle.class);
       if (paddle != null) {
           deltaY = -deltaY;
           int offset = getX() - paddle.getX();
           deltaX = deltaX + (offset/10);
           if (deltaX> 7) {
               deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
            SquareWorld mySquareWorld = (SquareWorld) getWorld();
            mySquareWorld.score();
          
   }
}
   
   /**
    * Move the Robot a given distance sideways
    */
   public void move(int dist)
   {
       setLocation (getX() + dist, getY());
       
   }
   
   /**
    * Put out a puff of smoke (only on every second call).
    */
   private void makeSmoke()
   {
       count--;
       if (count == 0) {
           getWorld().addObject ( new Smoke(), getX(), getY());
           count = 2;
   }
}
   
   /**
    * Release the Robot from the paddle.
    */
   public void release()
   {
       deltaX = Greenfoot.getRandomNumber(11) - 5;
       deltaY = -5;
       fixed = false;
   }
   

}


 
 
 

    
    

JetLennit JetLennit

2013/5/10

#
You need to change
Actor egg = getOneIntersectingObject(goldEgg.class);  
         
       if (goldEgg != null)  
       {  
           getWorld().removeObject(goldEgg);  
           deltaY = -deltaY;  
       }  
    }  
to
Actor egg = getOneIntersectingObject(goldEgg.class);  
         
       if (egg != null)  
       {  
           getWorld().removeObject(goldEgg);  
           deltaY = -deltaY;  
       }  
    }  
geekykid2013 geekykid2013

2013/5/10

#
thanks JetLinnit
geekykid2013 geekykid2013

2013/5/11

#
I'm now trying to add points to my counter. but every time I compile I receive an error message. what can i do?
java.lang.NullPointerException
	at Robot.checkEggs(Robot.java:91)
	at Robot.act(Robot.java:40)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.NullPointerException
	at Robot.checkEggs(Robot.java:91)
	at Robot.act(Robot.java:40)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
	at Robot.checkEggs(Robot.java:85)
	at Robot.act(Robot.java:40)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/5/11

#
JetLennit wrote...
You need to change
Actor egg = getOneIntersectingObject(goldEgg.class);  
         
       if (goldEgg != null)  
       {  
           getWorld().removeObject(goldEgg);  
           deltaY = -deltaY;  
       }  
    }  
to
Actor egg = getOneIntersectingObject(goldEgg.class);  
         
       if (egg != null)  
       {  
           getWorld().removeObject(goldEgg);  
           deltaY = -deltaY;  
       }  
    }  
Change line 5 in the second code block to
getWorld().removeObject(egg);
JetLennit JetLennit

2013/5/11

#
Missed that!
geekykid2013 geekykid2013

2013/5/12

#
I don't seem to get my counter to add a score of 5 points every time it collects a goldEgg object?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The Robot of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author  
 * @version (Version 1.0)
 */
public class Robot extends Actor
{
   
   private int points = 0;
   private Counter counter;
   
   public Robot(Counter pointsCounter)
   {
       counter = pointsCounter;
   }
    
   private int deltaX; //  delat is movement in the x direction
   private int deltaY; //  delta is movement in the y direction
   private int count = 2;
   
   private boolean fixed = true; // ????
   
   public Robot()
   {
   }
   
   /**
    * Act. Move the Robot if we're not fixed to paddle.
    */
   public void act()
   {
       if (!fixed)
       {
           move();
           makeSmoke();
           checkOut();
           checkEggs();
       }
   }
   
   /**
    * Move the Robot. Thencheck what we've hit.
    */
   public void move()
   {
       setLocation (getX() + deltaX, getY() + deltaY);
       checkPaddle();
       checkWalls();
   }
   
   /**
    * Check whether we've hit one of the three walls. Reverse direction
    */
   private void checkWalls()
   {
       if (getX() == 0 || getX() == getWorld().getWidth()-1) {
        deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
   }
   
   /**
    * Check whether we're out (bottom of screen).
    */
   private void checkOut()
   {
       if (getY() == getWorld().getHeight()-1) {
        ((SquareWorld) getWorld()).RobotIsOut();
        getWorld().removeObject(this);
        }
   }
   
   
   /**
    * Check whether we have hit an egg, and make the egg disappear if we have
    */
   
   private void checkEggs()
   {
       Actor egg = getOneIntersectingObject(goldEgg.class);
       
       if (egg != null)
       {
           getWorld().removeObject(egg);
           deltaY = -deltaY;
           counter.add(5);
       }
       
       //if (counter.getValue() >= 60) {
         //  gameOver();
        }
   
    
   
   private void checkPaddle()
   {
       Actor paddle = getOneIntersectingObject(Paddle.class);
       if (paddle != null) {
           deltaY = -deltaY;
           int offset = getX() - paddle.getX();
           deltaX = deltaX + (offset/10);
           if (deltaX> 7) {
               deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
            //SquareWorld mySquareWorld = (SquareWorld) getWorld();
            //mySquareWorld.score();
          
   }
}
   
   /**
    * Move the Robot a given distance sideways
    */
   public void move(int dist)
   {
       setLocation (getX() + dist, getY());
       
   }
   
   /**
    * Put out a puff of smoke (only on every second call).
    */
   private void makeSmoke()
   {
       count--;
       if (count == 0) {
           getWorld().addObject ( new Smoke(), getX(), getY());
           count = 2;
   }
}
   
   /**
    * Release the Robot from the paddle.
    */
   public void release()
   {
       deltaX = Greenfoot.getRandomNumber(11) - 5;
       deltaY = -5;
       fixed = false;
   }
   

}


 
 
 

    
    

danpost danpost

2013/5/12

#
One thing I would change is line 32, which I would make:
if (getWorld() != null) checkEggs();
You need to login to post a reply.