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

2012/8/5

how to make a timer count down

CrazyGamer1122 CrazyGamer1122

2012/8/5

#
i am working on a game that has "powerups" in it. the problem is that i need some kind of internal timer in the powerup class so after a certain time, the effect wears off. i would really appreciate help.
CrazyGamer1122 CrazyGamer1122

2012/8/5

#
here's a sample of how i figure the code will look--
public int time = 15;

public void CountTime()
{    
   //this is where the code for counting the time  will be 
   
}

public void TimeUp()
{
     if time < 0
        Disappear();
}

then i will have a disapper() method and a method to describe what the powerup does.
danpost danpost

2012/8/5

#
All you need is an instance integer variable in the powerup class to count down the act frames. In the class, but outside any method, add
int timer = 0;
You could also add another instance integer variable of final type to specify the maximum timer value:
final int MAX_TIMER_VAL = 500;
// adjust the value as needed
Then, in the act method, you should have something like:
if (timer > 0) timer--;
Now, you could use the timer to see if the powerup is active or not (if the value of the timer is zero, the powerup is NOT active), and you need some condition to start the powerup, which would then set timer with
timer = MAX_TIMER_VAL;
unless you create an instance of 'powerup' when the condition happens, in which case you would declare timer equals to MAX_TIMER_VAL, instead of zero, at the start; and remove the 'powerup' object when the timer hits zero.
CrazyGamer1122 CrazyGamer1122

2012/8/5

#
the idea is that the person shoots the enemy, which drops the powerup. the timer starts when the player touches (picks up) the powerup.
danpost danpost

2012/8/6

#
Then, maybe you should put the timer int in the player class. Set the timer to MAX_TIMER_VAL and remove powerup object when intersection occurs. You can still use the value of timer to check if powerup is active or not.
CrazyGamer1122 CrazyGamer1122

2012/8/6

#
thanks
You need to login to post a reply.