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

2013/4/9

Trouble with getting scoreboard to work

Griffon305 Griffon305

2013/4/9

#
So I am currently working on an asteroid game where the player has to protect four homes from oncoming asteroids. It is nearly finished but I am having some trouble making the game end. I want to make the game so that if three of the four homes are destroyed, the game ends and a message pops up saying how many asteroids you destroyed and how many bullets you used. I can't seem to get the message to pop up or even for the game to end. Here's the code for the asteroid subclass:
public class asteroid extends movers
{
    /**
     * Act - do whatever the asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int speed = 2;
    public static int asteroidRotation = 90;
    public int houseCount = 0;
   
    
    public void act()
    {
        setRotation(asteroidRotation);
        move(speed);
        hitHome();
        checkCount();
    }
    
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    public void destroy(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
        }
    }
    
    public void hitHome()
    {
       
       if (canSee(home.class))
       {
       //getWorld().addObject(new explosion(), getX(), getY());
       destroy(home.class);
       getWorld().removeObject(this);
       houseCount = houseCount + 1;
       }
    }
    
    public void checkCount()
    {
    if(houseCount == 3) 
       {
           
           getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);     
           Greenfoot.stop();
        }
    }
The two relevant bits of code, the hitHome() method and checkCount() method are at the bottom. I have a counter that should be increasing by one (called houseCount) everytime a house is hit by the asteroid. In theory, once this value equals three the game is supposed to end a message pop up. Here's the code for the pop up message (I took it from a discussion board on the greenfoot site and made a couple minor adjustments):
import greenfoot.*;  
import java.awt.Color;
public class GameOver extends SubActor

{  
    String text = " GAME OVER! You destroyed "+ hitCount +" asteroids and used " + bulletCount + "bullets!";  
    GreenfootImage inner = new GreenfootImage(text, 48, Color.black, new Color(0, 0, 0, 96));  
  
    public GameOver()  
    {  
    }  
  
    // create a gameover image the size of the world  
    public void addedToWorld(World world)  
    {  
        int wide = world.getWidth();  
        int high = world.getHeight();  
        GreenfootImage outer = new GreenfootImage(wide, high);  
        int leftX = (wide - inner.getWidth())/2;  
        int topY = (high - inner.getHeight())/2;  
        outer.drawImage(inner, leftX, topY);  
        setImage(outer);  
    }  
  
    // the following is optional  
    // remove pop-up when clicked on  
    public void act()  
    {  
        if (Greenfoot.mouseClicked(this))  
        {  
             getWorld().removeObject(this);  
        }  
    }  
}  


Let me know if you need anything more information. I may not be able to respond until I get back from work tonight. Thank you!
Gevater_Tod4711 Gevater_Tod4711

2013/4/9

#
I think the problem is that the variable house count is in the asteroid and if the asteroid is distroyed or if there is a new asteroid the variable is 0 again and so it'll never reach 3. You should declare the variable static. Then it'll be counted up by every asteroid.
Griffon305 Griffon305

2013/4/9

#
Great! It worked (mostly). I can get the game to stop but now I get an error message in the terminal window that says: java.lang.NullPointerException at asteroid.hitHome(asteroid.java:57) at asteroid.act(asteroid.java:24) 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) I didn't paste all of the code in my first post, so actually line 57 and 24 are as follows (line 57 is found in the hitHome() method)
if(houseCount > 2) 
       {
           
           **line 57**getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
           Greenfoot.stop();
        }
(line 24 is found in the act() method)
public void act()
    {
        setRotation(asteroidRotation);
        move(speed);
        //line 24:
hitHome();
        
        
    }
Griffon305 Griffon305

2013/4/9

#
Sorry I posted before I could finish, so any thoughts on how to get rid of the error message?
Gevater_Tod4711 Gevater_Tod4711

2013/4/9

#
The problem is that the hit home mehtod destroyes the asteroid (getWorld().removeObject(this)) and then in the check count method you again call getWorld() but the asteroid is not existing in any world so getWorld() returns null and you get this exception.
Griffon305 Griffon305

2013/4/9

#
Is there a way I can have my GameOver window open without using the getWorld() method?
Gevater_Tod4711 Gevater_Tod4711

2013/4/9

#
No I don't think so. But if you change to order of the hitHome and checkCounter method it should work.
You need to login to post a reply.