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

2018/3/19

Show score at the end of the game

R0mane R0mane

2018/3/19

#
Hello, I am making a game where the actor has to stay on screen as long as possible and not get touched by the balls. I have a timer that works, but when the ball touches the actor, the score (in seconds) should appear but what appears is only "your score : 0.0". How can I change the 0.0 and make it the actual score? Here is my code for the timer class:
public class Timer extends Actor
{
    private long startTime; 
    private long stopTime;
    private long scoreTime;
    private double elapsedTime; 
    private int a = 0;
    private int b = 0;
    GreenfootImage time;
    private static double score;
    public void act() 
    {
        if (a == 0){
            startTime = System.currentTimeMillis();
            a = 1; 
        }
        updateTime();
        endTime();
    }    

    private void updateTime()
    {
        stopTime = System.currentTimeMillis(); 
        elapsedTime = (stopTime-startTime)/1000.0; 
        time = new GreenfootImage("Timer: " + new DecimalFormat("0.0").format(elapsedTime), 30, Color.WHITE, null, Color.BLACK); // makes the timer image, the decimal format makes the timer only have one decimal place (0.0) (you could do more if you do (0.00) etc.
        setImage(time);
    }

    private void endTime(){
        
        if (getWorld().getObjects(Player1.class).isEmpty()){
            
            score = (scoreTime - startTime)/1000.0;
        }
    }

    public static double getScore(){
        return score;
    }
}
Can you help me please?
danpost danpost

2018/3/19

#
You will probably need to show your World subclass codes.
R0mane R0mane

2018/3/20

#
static double score = Timer.getScore();
    
    private void endGame (){
        GreenfootImage background;
        background = this.getBackground();
        
        Gameover gameover = new Gameover();
        this.addObject(gameover, 300, 100);
        this.removeObject(timer);
        
        Color aColor = new Color(76, 0, 153);
        Font aFont = new Font("Arial", Font.BOLD, 30);
        
        background.setColor(aColor);
        background.setFont(aFont);
        
        background.drawString("Your score : " + score, 200, 225);
    }
That is the method that happens at the end of the game
Vercility Vercility

2018/3/20

#
Remove line 31 and 34 Also,next time don't create 2 threads, just state both your problems in one.
danpost danpost

2018/3/20

#
In the Background class (endGame method), remove line 1 and change score in line 17 to timer.getScore(). Remove the world static from lines 10 and 37 in the Timer class.
R0mane R0mane

2018/3/20

#
Thanks a lot
You need to login to post a reply.