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

2014/2/21

Score Board Help Needed

Osidy Osidy

2014/2/21

#
Hello, I am having trouble figuring out how to make a live score board show the number of crates delivered to the depot. I do not understand how to get the number of crates to transfer to the counter class. I will post my code for my world, depot, and counter classes below. World:
private void prepare()
    {

        Counter scoreCounter = new Counter();
        addObject(scoreCounter, 59, 15);

        Depot depot = new Depot(scoreCounter);
        addObject(depot, 50, 62);

        Crate crate = new Crate();
        addObject(crate, 369, 101);
        Crate crate2 = new Crate();
        addObject(crate2, 201, 377);
        Crate crate3 = new Crate();
        addObject(crate3, 153, 191);

        Barrier barrier = new Barrier();
        addObject(barrier, 370, 244);
        barrier.setLocation(397, 185);
        Barrier barrier2 = new Barrier();
        addObject(barrier2, 398, 286);
        Barrier barrier3 = new Barrier();
        addObject(barrier3, 398, 387);
        Barrier barrier4 = new Barrier();
        addObject(barrier4, 189, 291);
        barrier4.setRotation(90);
        Barrier barrier5 = new Barrier();
        addObject(barrier5, 612, 296);
        barrier5.setRotation(90);

        Truck truck = new Truck();
        addObject(truck, 63, 63);
        truck.setRotation(0);
    }
}
Counter;
public class Counter extends Actor
{
    private GreenfootImage background;
    private Color color;
    private Color transparent;
    public int score;
    public Counter()
    {
        score=0;
        background=getImage();
        color=new Color(0,0,0,255);
        transparent=new Color(0,0,0,0);
        update();
    }
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       update(); // Add your action code here.
    }
    
    public void update()
    {
        GreenfootImage image=new GreenfootImage(background);
        GreenfootImage textScore=new GreenfootImage("Crates:"+ score, 22, color, transparent);
        image.drawImage(textScore,10,3);
        setImage(image);
    }
    
    public int getScore()
    {
        return score;
    }
}
Depot:
public class Depot extends Cars
{
    int score=0;
    private Counter counter;
    /**
     * Depot Constructor
     */
    public Depot(Counter scoreCounter)
    {
        counter=scoreCounter;    
    }
    
    /**
     * Act - do whatever the Depot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (canSee(Crate.class)==true)
        {
            eat(Crate.class);
            score++;
        }
        if (score>2)
        {
           
        }
    }
    
    public int getScore()
    {
        return score;
    }
}
I have tried everything I can think of, and have made no progress. Any help is welcome! Thanks!
Osidy Osidy

2014/2/21

#
Lines 24-27 of depot are for new levels in the future.
danpost danpost

2014/2/21

#
First off, you should only have one field to hold the score; remove the 'score' field and the 'getScore' method from the Depot class. Next, you will need a method in the Counter class that will change the value of the 'score' field within the class (maybe called 'bump' to add one and update). Then, in the Depot class, you can use 'counter.bump();' to increment the score and 'counter.getScore()' to get the score
Osidy Osidy

2014/2/22

#
Ohhh, duh! Thank you so much!
You need to login to post a reply.