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

2013/10/25

Need help getting things to show up on the score board.

wiserviser wiserviser

2013/10/25

#
The objective of the game is to use a turret to shoot cannonballs to hit a target. My scoreboard should display the angle the turret it shooting at, the power it is shooting at, and the number of missed times that the cannonball was not able to hit the target. I got the power and the angle to work and show up on the statusboard, but the number of misses will not show up. If i pause the game and check manually the number of misses, i can get it -but it wont show up on the status board. I did some editing and now the game just stops a second after i shoot the cannonball and a nasty "Greenfoot: Terminal Window java.lang.NullPointerException error shows up.. D: StatusBoard class:
public class StatusBoard extends Actor
{

    protected GreenfootImage background = null; 
    
    public void act() 
    {
        // Add your action code here.
        
    }    
    
    
    
    public StatusBoard()
    {
        background = new GreenfootImage (100, 180);
        background.setColor(new java.awt.Color(255, 255, 255, 200));
        background.fillRect(0,0,150,60);
        setImage(background);
    }
    
    public void setValues (int angle, int power, int misses)
    {
        GreenfootImage img = new GreenfootImage (background); 
        img.setColor (java.awt.Color.BLACK);
        
        img.drawString("Angle: " + angle, 10, 20); 
        img.drawString("Power: " + power, 10, 35); 
        img.drawString("Missed:  " + misses,  10, 50);
        setImage(img); 
    }
}
CannonBall Class
public class CannonBall extends Actor
{

    public static final int GRAVITY = 2; 
    protected int angle = 0;
    protected int power = 100;
    protected int misses = 0;
    protected StatusBoard statusBoard;
    protected int xSpeed = 0; 
    protected int ySpeed = 0; 
    
    public void act() 
    {
        // Add your action code here.
        move(1);
        setLocation (getX() + xSpeed, getY() - ySpeed);
        ySpeed = ySpeed - GRAVITY; 
        destroyCannonBall();
        statusBoard.setValues (angle, power, misses); 
    }
    
    public CannonBall (StatusBoard board)
    {
        statusBoard = board;
    }
    
    
    public CannonBall (int angle, int power)
    { 
        power = (power * 10) / 25; 
        xSpeed = (int)(power*Math.cos(Math.toRadians(angle))); 
        ySpeed = (int)(power*Math.sin(Math.toRadians(angle))); 
    } 
    
    
    //getting cannon ball to dissapear once its reached the edge of the world 
   public void destroyCannonBall()
   {
        FodderWorld fodderWorld = (FodderWorld)getWorld(); 
        if (getY() > fodderWorld.getHeight()) 
        { 
            fodderWorld.removeObject(this);
            fodderWorld.gainMiss();
        }
   }
}
I'm not sure if you need this, because this part actually works, but this is the Turret Class
public class Turret extends Actor
{
 
    protected int angle = 0;
    protected int power = 100;
    protected int misses = 0;
    protected StatusBoard statusBoard;
    
    public void act() 
    {
        // Add your action code here.
        aimTurret();
        statusBoard.setValues (angle, power, misses); 
        fireCannonBall();
    }    
    
    public Turret()
    {
        setRotation(300);
    }
    
    public Turret(StatusBoard board)
    {
        statusBoard = board;
    }
    
    //This method will allow the player to aim the turret 
    public void aimTurret()
    {
        if (Greenfoot.isKeyDown ("left") && (angle < 90))
        {
            angle = angle+1;
            setRotation(-angle);
        }
        
        if (Greenfoot.isKeyDown ("right") && (angle > 0))
        {
            angle = angle-1;
            setRotation(-angle);
        }
        
        if (Greenfoot.isKeyDown ("up") && (power < 100))
        {
            power = power+1;
        }
        
        if (Greenfoot.isKeyDown("down") && (power > 0))
        {
            power = power-1;
        } 
        
    }
    
   public void fireCannonBall()
   {
        if (Greenfoot.isKeyDown("space") && noCannonBalls())
        { 
            World cannonWorld = getWorld(); 
            CannonBall ball = new CannonBall (angle, power); 
            cannonWorld.addObject(ball, getX(), getY()); 
        } 
   }

    public Boolean noCannonBalls () 
    {
        { 
            World cannonWorld = getWorld(); 
            java.util.List balls = cannonWorld.getObjects(CannonBall.class); 
            return balls.isEmpty(); 
        }
    }
}
What am i doing wrong? D: Thanks in advanced, guys.
GreenHouse GreenHouse

2013/10/25

#
The Cannonball's fodderWorld.gainMiss(); tells the world that the ball missed the target, right? But the protected int misses isn't increased. Maybe you just forgot to misses++ after fodderWorld.gainMiss(); ?
danpost danpost

2013/10/25

#
Probably need to see the fodderworld class also (to see how you create/pass objects along). However, I do not (as far as I can tell) see any need for more than one constructor in the Turret class or the CannonBall class. You should create just one scoreboard and pass it to one turret during creation, then pass all three items (the angle, the power and the scoreboard) to each cannonball object.
GreenHouse GreenHouse

2013/10/25

#
By the way, use private instead of protected :) Protected doesn't make sense in here.. You can also set int misses to public, so the world class can increase the value.
wiserviser wiserviser

2013/10/25

#
It'll calculate the misses if I stop the game and check it manually, so the misses are being calculated but they're just not being translated onto the status board. Here is the fodderworld class..
public class FodderWorld extends World
{
    private int misses = 0; //number of misses to start with.
    private int score =0; //score to start with 
 
    public FodderWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);  
        Greenfoot.start();
        
        StatusBoard status = new StatusBoard ();
        status.setValues (0, 100, 0);
        addObject (status, 50, 90);
        
        
        int height = Greenfoot.getRandomNumber(5) + 1;
        for (int index = 0; index < height; index++) 
        { 
            addObject (new Wall(), 285, 380-(50*index)); 

        } 

        
        Turret turret = new Turret (status);
        CannonBall cannonball = new CannonBall (status); 
        
        addObject(turret, 20 + Greenfoot.getRandomNumber(200), 380);
        
        addObject(new Target(), 340 + Greenfoot.getRandomNumber(230), 375);
        
        gainMiss();
        getMiss();
       
        

    }    
    
   
    public void gainMiss()
    {
        misses = misses+1;
    }
    
    public int getMiss()
    {
        return misses;
    }
    
    public void increaseScore()
    {
        score = score+10;
    }
    
    public int getScore()
    {
        return score;
    }
}
I'm trying to compare what I did to the turret class to the cannonball class because i got the turret to connect to the statusboard, but i cant get the cannonball class to connect. D: Thanks again!
danpost danpost

2013/10/25

#
It might be better, since the turret object is added once and never removed, to put everything in that class (score, misses, angle and power) with their respective methods (getScore, increaseScore, gainMiss) plus other methods (getAngle, getPower, getMisses). Do not create a cannonball in the world class as they get created in the Turret class when triggered by the user pressing a key. Then, you will only have to pass the turret object to the cannonballs as you create them and use the new methods to acquire the information needed and use the methods you moved from the world class to adjust the values of score and misses.
GreenHouse GreenHouse

2013/10/27

#
wiserivcer; you have multiple int misses: Once in Fodderworld.class (this is the int misses, that is increased in the method gainMiss() ) and once in Turret.class (this is the int misses, that is used for updating your scoreboard by statusBoard.setValues() ) Because the second one is never increased, the score doesn't change...
You need to login to post a reply.