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

2017/4/2

ScoreBoard Error

AAA355 AAA355

2017/4/2

#
i am trying to make a ascoreboard that counts up whenever i shoot a "robber" and count down everytime a robber collects a "moneyBag". i have used the same code i have used on other games but it still wont work. Bullet code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends bullets
{
     public int speed = 10;
    int score = 0;
    
    /**
     * Act - do whatever the missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(15);
        Actor Robber = getOneIntersectingObject(Robber.class);
        getWorld().showText("robbers killed: " + score, 96, 46);
        if(isTouching(Robber.class))
        {
            World myWorld = getWorld();
            getWorld().removeObject(Robber);
            getWorld().addObject(new Robber(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            Background background =(Background)myWorld;
            scoreboard scoreboard = Background.getscoreboard();
            scoreboard.addScore();
            getWorld().removeObject(this);

        }
        }
}
Robber Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Robber here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Robber extends people
{
    int score = 0;
    
    /**
     * Act - do whatever the man1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move(Greenfoot.getRandomNumber(10));
        turn(10 - Greenfoot.getRandomNumber(20));
        getWorld().showText("Money Bags lost: " + score, 296, 46);
        if(getX() <=5 || getX() >= getWorld().getWidth() - 5)
        {
            
            turn(90);
            
        }
        
         if(getY() <=5 || getY() >= getWorld().getHeight() - 5)
        {
            
            turn(90);
            
        }
        
        Actor MoneyBag;
        
        MoneyBag = getOneObjectAtOffset(0,0,MoneyBag.class);
        
        if(MoneyBag != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(MoneyBag);
            Background background =(Background)myWorld;
            scoreboard scoreboard = background.getScoreboard();
            scoreboard.SubtractScore();
            getWorld().addObject(new MoneyBag(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));

        }
        
        Actor Player;
        
        Player = getOneObjectAtOffset(0,0,Player.class);
        
        if(isTouching(Player.class))
        {
            World world = getWorld();
            if (world != null) {
                world.removeObjects(world.getObjects(null));
                world.setBackground("game_over.png");
                
            }
            
                       
        }
       
        
        
    }     
}
Player Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends people
{
    int score =0;
    private final int reloadTime = 300,
                  maxShots = 6;
    private int reloadTimer = 100,
            shotsLeft = maxShots;
    private boolean shooting = false;
    
    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getWorld().showText("Robbers Killed: " + score, 96, 46);
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
        
        if (reloadTimer > 50)
        {
            if (--reloadTimer == 50) shotsLeft = maxShots;
            else return;
        }
        
        if (!shooting && Greenfoot.isKeyDown("space"))
        {
            Actor Bullet = new Bullet();
            getWorld().addObject(Bullet, getX(), getY());
            Bullet.setRotation(getRotation());
            shooting = true;
            if (--shotsLeft == 0 ) reloadTimer = reloadTime;
        }
        else if (!Greenfoot.isKeyDown("space")) shooting = false;

        }
        
    }
ScoreBoard Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class scoreboard extends display
{
    int score = 0;
    /**
     * Act - do whatever the scoreboard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("score : " + score, 36, Color.BLACK, Color.WHITE));
    }    
    
    public void SubtractScore(){
       score--;
    }
    public void addScore(){
        score++;
    }
    }
thanks in advance.
AAA355 AAA355

2017/4/2

#
it says that the error is on line 46 of the Robber code and it has .getScoreBoaard underlined. same with the Bullet code on line 29
Nosson1459 Nosson1459

2017/4/2

#
There are a lot of things that don't make sense over here (you didn't say what the problem is "it says that the error... .getScoreBoaard underlined", that's all you said). In Bullet you have: scoreboard scoreboard = Background.getscoreboard(); but in Robber you have: scoreboard scoreboard = background.getScoreboard(); You are creating a new scoreboard in Robber, you're not setting the scoreboard in Background just getting it. Your classes should start with a capital letter and your variables and methods should start with a lowercase letter. Change the code in Robber to:
        Actor moneyBag;

        moneyBag = getOneObjectAtOffset(0, 0, MoneyBag.class);

        if (moneyBag != null) {
            getWorld().removeObject(moneyBag);
            ((Background) getWorld()).getScoreBoard().subtractScore();
            getWorld().addObject(new MoneyBag(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));

        }
Change the code in Bullet to:
        getWorld().showText("robbers killed: " + score, 96, 46);
        if (isTouching(Robber.class)) {
            removeTouching(Robber.class);
            getWorld().addObject(new Robber(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            ((Background) getWorld()).getScoreBoard().addScore();
            getWorld().removeObject(this);
        }
AAA355 AAA355

2017/4/2

#
thanks for the reply, I have made all the changes and I still have the same error. Robber code:
Actor MoneyBag;
        
        MoneyBag = getOneObjectAtOffset(0,0,MoneyBag.class);
        
        if(MoneyBag != null){
            getWorld().removeObject(MoneyBag);
            ((Background) getWorld()).getScoreBoard().subtractScore();
            getWorld().addObject(new MoneyBag(),Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            
        }
.getScoreBoard is still underlined and it says that the error is: cannot find symbol - method getScoreBoard()
danpost danpost

2017/4/2

#
You will need to show your Background class codes.
AAA355 AAA355

2017/4/2

#
ScoreBoard code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScoreBoard extends display
{
    int score = 0;
    /**
     * Act - do whatever the scoreboard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("score : " + score, 36, Color.BLACK, Color.WHITE));
    }    
    
    public void SubtractScore(){
       score--;
    }
    public void addScore(){
        score++;
    }
    }
background code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class background here. * * @author (your name) * @version (a version number or a date) */ public class Background extends World { /** * Constructor for objects of class background. * */ public Background() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1200, 900, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Player player = new Player(); addObject(player,80,808); Robber robber = new Robber(); addObject(robber,1103,68); Robber robber2 = new Robber(); Robber robber4 = new Robber(); addObject(robber4,539,72); Robber robber5 = new Robber(); addObject(robber5,1127,442); Robber robber6 = new Robber(); addObject(robber6,542,445); Robber robber7 = new Robber(); addObject(robber7,833,272); Robber robber8 = new Robber(); addObject(robber8,302,267); Robber robber9 = new Robber(); addObject(robber9,796,652); MoneyBag moneybag = new MoneyBag(); addObject(moneybag,973,377); MoneyBag moneybag2 = new MoneyBag(); addObject(moneybag2,683,174); MoneyBag moneybag3 = new MoneyBag(); addObject(moneybag3,645,387); MoneyBag moneybag4 = new MoneyBag(); addObject(moneybag4,645,387); MoneyBag moneybag5 = new MoneyBag(); addObject(moneybag5,378,359); MoneyBag moneybag6 = new MoneyBag(); addObject(moneybag6,651,588); MoneyBag moneybag7 = new MoneyBag(); addObject(moneybag7,307,648); Timer timer = new Timer(); addObject(timer,1105,53); timer.setLocation(1108,32); ScoreBoard scoreboard = new ScoreBoard(); addObject(scoreboard,72,54); } } thanks in advance
danpost danpost

2017/4/2

#
You need to move:
ScoreBoard scoreboard = new ScoreBoard();
line to above your constructor documentation -- after:
public class Background extend World
{]/code]
Then you need to add the following before the last closing squiggly bracket:
[code]public ScoreBoard getScoreBoard()
{
    return scoreboard;
}
AAA355 AAA355

2017/4/2

#
thank you so much, it finally works.
You need to login to post a reply.