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

2018/3/20

Still problems with my timer

Kolyma Kolyma

2018/3/20

#
I tried to create the counter on this way now, but my timer just subtracts 1 second and then stops. I have to hand out this project in three hours and I am slowly in despair.
public class Level2 extends MyWorld
{
    Text timerText = new Text();
    private int timer = 15;
    /**
     * Level2 enthält einen Timer, der 15 Sekunden abzählt.
     * Wenn Zähler = 0 und Punktestand nicht 10, dann "Defeat"
     */
    public Level2()
    {
        prepare(); // Level2 wird mit den gleichen Actors aus MyWorld bevölkert
    }
    private void prepare()
    {
        addObject(timerText, 730 , 40);
        updateImage();
        timerText.setText("Time: " + timer); 
    }
    public void updateImage()
    {
        if (timer>0)
        {
            timer--;
            if(timer%15==0)
            {
              timerText.setText("Time: " + timer);
              updateImage();
            }        }
    }
}
public class Text extends Actor
{
    // Der Timer startet bei 15 Sekunden
    private static final Color transparent = new Color(0,0,0,0);
    /**
     * 
     */
    public Text() 
    {
        this(" ");
        
    }
    public Text(String text)
    {
        setText(text);
    }
    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 30, Color.BLACK, transparent));
       
    }
}
Vercility Vercility

2018/3/20

#
Ure only calling UpdateImage once If you want it to last 15 s use something like this
Long cur = System.currentTimeMillis;

While(Timer>0) {
  Long atm = System. CurrentTimeMillis;
  If(cur-atm >= 1000) {
 cur+1000;
////
}
} 
danpost danpost

2018/3/20

#
Vercility wrote...
If you want it to last 15 s use something like this << Code Omitted >>
Using while will halt the entire program as the timer runs. That behavior is rarely wanted. Also, while cannot be capitalized. @Kolyma, if you want your timer to run longer than 15 act cycles (probably about one-quarter of a second), you need to increase its initial value. Something like 900 act cycles would run for about 15 seconds. Change %15 to %60 in line 24 and change timer to (timer/60) in lines 17 and 26.
Vercility Vercility

2018/3/20

#
danpost wrote...
Vercility wrote...
If you want it to last 15 s use something like this << Code Omitted >>
Using while will halt the entire program as the timer runs. That behavior is rarely wanted. Also, while cannot be capitalized. @Kolyma, if you want your timer to run longer than 15 act cycles (probably about one-quarter of a second), you need to increase its initial value. Something like 900 act cycles would run for about 15 seconds. Change %15 to %60 in line 24 and change timer to (timer/60) in lines 17 and 26.
Hmm does it, thats interesting. And yeah, i was using my phone so autocorrect fuqqs me up :(
You need to login to post a reply.