Empty timer check
Hi , iam using thejava.util.Timer for a game iam creating , ive created an array of timers to keep track of how long a special item lasts for in the game and for a respawn time aswell. My problem is looping through the aray of timers to find one that aint in use so i can assign it to do a diffrent task , Heres the code...
publicvoid ItemSpecialHandling(int specialID){
/*
1 = SPEED
2 = GOD
3 = BOMB
*/
int empty_timer;//The timer number which will be used
empty_timer = getEmptyTimer(timer);//Assign a number of a timer that isnt in use to empty_timer
if (specialID == 1){//If the box is a speedbox
timertest = empty_timer;//TESTING PURPOSE , DISPLAY THE EMPTY TIMER NUMBER
timer[empty_timer] =new Timer();//Create a new timer
speed =true;//Set speed to true
timer[empty_timer].schedule(new SpecialItemTask(specialID, timer[empty_timer]), 15000);// Start the timer!
timer[empty_timer] =null;//destroy the timer after
}elseif (specialID == 2){//If the box is a God box
timertest = empty_timer;//TESTING PURPOSE , DISPLAY THE EMPTY TIMER NUMBER
timer[empty_timer] =new Timer();//Create a new timer
player1.setGod(true);//Set god mode to true
timer[empty_timer].schedule(new SpecialItemTask(specialID, timer[empty_timer]), 30000);//Start the timer!
timer[empty_timer] =null;//destroy the timer after
}
}
publicint getEmptyTimer(Timer[] timer){
for (int i = 0; i < timer.length; i++)//Loop through all the timers
{
if (timer[i] !=null)//if the timer is not NULL then return the number
{
return i;
}
}
return 0;//If there all in use return 0
}
class SpecialItemTaskextends TimerTask{
int ID;
Timer TimerToStop;
SpecialItemTask(int SpecialID, Timer timer){
TimerToStop = timer;
ID = SpecialID;
}
publicvoid run(){
if (ID == 1){
speed =false;
}elseif (ID == 2){
player1.setGod(false);
}
TimerToStop.cancel();
}
}
Sorry iam still new to java so some of it might not make sense at all ... but my problem is that the getEmptyTimer(Timer[] timer) always returns '0'.
Any help at all would be appricated fully.
Thanks in Advance.

