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

2018/2/27

I want to create a timer

raging_ewok raging_ewok

2018/2/27

#
Hi, I would like to create a timer in my space shooter game but do not know how. I would like it to go up in seconds and then stop until my Alien sprite has killed my Spaceship sprite.
danpost danpost

2018/2/27

#
Please show what you tried. Do you even know how to declare a field?
raging_ewok raging_ewok

2018/2/27

#
I do know how to declare a field but I have not attempted to create a timer. I Iooked at another forum but it just wouldn't work for my game.Here is the forum
danpost danpost

2018/2/27

#
Use something like this:
// field
private int timer;

// in constructor
updateTimer();

// in act
if (++timer%60 == 0) updateTimer();

// add this method
private void updateTimer()
{
    showText("Time: "+(timer/3600)+"m "+(timer/60)+"s", 80, 20);
}
raging_ewok raging_ewok

2018/2/27

#
Which class do I put this in?
danpost danpost

2018/2/27

#
raging_ewok wrote...
Which class do I put this in?
World class ... but I was about to edit it to this:
// field
private int timer;
 
// in constructor
updateTimer();
 
// in act
if (timer >= 0 && ++timer%60 == 0) updateTimer();
 
// add these methods
private void updateTimer()
{
    showText("Time: "+(timer/3600)+"m "+(timer/60)+"s", 80, 20);
}

public void stopTimer()
{
    timer = -1;
}
raging_ewok raging_ewok

2018/2/27

#
You leg end. Thank you very much danpost!
danpost danpost

2018/2/27

#
Line 13 should be:
showText("Time: "+(timer/3600)+"m "+((timer%3600)/60)+"s", 80, 20);
You need to login to post a reply.