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.

[4645 byte] By [Riskeha] at [2007-10-2 8:41:16]
# 1

I don't see the point of this array. You seem to create Timers and use them correctly so I don't understand why you want to temporarily assign them to an array variable.

At any rate, your code starts with all of the array entries being null. Your code runs through the array looking for non-null entries and it doesn't find any. So it returns 0.

Is it really the case that "not in use" is represented by "not null" in the array? I would have thought that "not in use" would be represented by "null". Especially since as far as I can see, your array will always contain all nulls when you query it.

DrClapa at 2007-7-16 22:43:15 > top of Java-index,Java Essentials,Java Programming...
# 2
And how do you distinguish the zero returned when all timers are in use from the zero returned when timer number zero is not in use?
DrClapa at 2007-7-16 22:43:15 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't think you should be doing that. I would suggest that what you are trying to achieve is premature optimisation. Are you worried about the overhead of creating a new timer?

As well, I would suggest that you take the time to go through the sun java coding standards guidelines (for your own sake), so that it will be easier for others to follow your code.

macrules2a at 2007-7-16 22:43:15 > top of Java-index,Java Essentials,Java Programming...