Drofder2004 wrote:Just remove the entire line from the code, it is only put there as a small 'fun factor' and is not crucial to the working.
If you DO want to keep it, add
To a new line above it.
**:
because we want to display it only if the timer is still running.
Drofder2004 wrote:In theory, it probably isn't as program heavy as it looks as there is still only ever one loop running, but just for simplicity it is out there in the over-complicated stages
As long as it makes sense to me I'm fine with it.
--
Anyway, here's your updated code, it should work as intended now:
Code: Select all
/*
- Call playerInit(), stopwatchInit(x, y, offset) and miscHUDinit(offset) on a player
(eg. stopWatchInit(100, 0, 30);, miscHUDinit(35); )
- When you're ready to start the stopwatch, set isTiming to true and thread monitorTime()
- When you're done with the timing, simply set isTiming to false
*/
init()
{
precachestring(&":");
level thread onPlayerConnect();
}
onPlayerConnect()
{
while( true )
{
level waittill("connecting", player);
player.isTiming = false;
player thread onPlayerSpawned();
}
}
onPlayerSpawned()
{
self endon("disconnect");
while(true)
{
self waittill( "menuresponse", menu, response );
if( menu == "cj" )
{
if( response == "stopwatchstart") {
if(!self.isTiming)
{
self playerInit();
self stopWatchInit(100, 0, 30);
self miscHUDinit(35);
self startTiming();
} else self iprintln("Stopwatch already running!");
}
if( response == "stopwatchstop")
if(self.isTiming)
self stopTiming();
else self iprintln("Stopwatch hasn't started!");
if( response == "stopwatchdestroy")
self destroyStopwatch();
}
wait 0.5;
}
}
playerInit()
{
if(!isDefined(self.stopwatchCounter))
{
self.stopwatchCounter = [];
self.stopwatchCounter[0] = 0; //seconds
self.stopwatchCounter[1] = 0; //minutes
self.stopwatchCounter[2] = 0; //hours
//(could've used the loop below but wanted to put more emphasis on what each element does)
}
if(!isdefined(self.stopwatch) && !isdefined(self.misc))
{
self.stopwatch = [];
self.misc = [];
for(i=0;i<3;i++)
{
self.stopwatch[i] = newclientHUDelem(self);
if(i<2) self.misc[i] = newclientHUDelem(self);
}
}
}
/*x and y are self explanatory, offset is the length between each element(sec, min, hr) */
stopwatchInit(x_v, y_v, offset)
{
for(i=0;i<self.stopwatch.size;i++)
{
if(i==0) self.stopwatch[i].alignX = "left";
else self.stopwatch[i].alignX = "center";
self.stopwatch[i].alignY = "middle";
self.stopwatch[i].horzAlign = "left";
self.stopwatch[i].vertAlign = "middle";
if(i==0)
{
self.stopwatch[i].x = x_v;
self.stopwatch[i].y = y_v;
}
else
{
self.stopwatch[i].x = self.stopwatch[i-1].x-offset;
self.stopwatch[i].y = self.stopwatch[i-1].y;
}
self.stopwatch[i].foreground = 1;
self.stopwatch[i].color = (1, 1, 1);
self.stopwatch[i].alpha = 1;
self.stopwatch[i].font = "default";
self.stopwatch[i].fontScale = 2;
}
updateStopwatchHUD();
}
miscHUDinit(offset) /*[0] is the : in between hours and minutes, [1] is the : inbetween minutes and seconds*/
{
for(i=0;i<self.misc.size;i++)
{
self.misc[i].alignY = "middle";
self.misc[i].alignX = "left";
self.misc[i].horzAlign = "left";
self.misc[i].vertAlign = "middle";
self.misc[i].font = "default";
self.misc[i].fontScale = 2;
self.misc[i].foreground = 1;
self.misc[i].color = (1, 1, 1);
self.misc[i].alpha = 0;
self.misc[i].x = self.stopwatch[i].x - (offset/2);
self.misc[i].y = self.stopwatch[i].y;
self.misc[i] setText(&":");
}
}
monitorTime()
{
self thread stopWatchColon();
while(self.isTiming)
{
for(h=0;h<24;h++)
{
self.stopwatchCounter[2] = h;
for(i=0;i<60;i++)
{
self.stopwatchCounter[1] = i;
for(j=0;j<600;j++)
{
self.stopwatchCounter[0] = j/10;
self updateStopwatchHUD();
wait 0.1;
if(!self.isTiming)
break;
}
if(!self.isTiming)
break;
}
if(!self.isTiming) //no 'goto' keyword to break out of all loops :(
break;
}
if(self.isTiming)
self iprintlnbold("You've been going for a day now, don't you think it's time to quit already?");
wait 0.05;
}
}
stopwatchColon()
{
while(self.isTiming)
{
if(self.misc[0].alpha==1)
self.misc[0].alpha = 0;
else self.misc[0].alpha = 1;
if(self.misc[1].alpha==1)
self.misc[1].alpha = 0;
else self.misc[1].alpha = 1;
wait 0.5;
}
}
updateStopwatchHUD()
{
if(isdefined(self.stopwatch))
{
for(i=0;i<self.stopwatch.size;i++)
self.stopwatch[i] setValue(self.stopwatchCounter[i]);
}
}
startTiming()
{
self.isTiming = true;
for(i=0;i<self.stopwatchCounter.size;i++)
{
self.stopwatchCounter[i] = 0;
self.stopwatch[i] setValue(self.stopwatchCounter[i]);
if(i<2) self.misc[i] setText(&":");
}
self thread monitorTime();
}
stopTiming()
{
self.isTiming = false;
self iprintln("Time: "+self.stopwatchCounter[2]+":"+self.stopwatchCounter[1]+":"+self.stopwatchCounter[0]);
}
destroyStopwatch()
{
if(isDefined(self.stopwatch) && isDefined(self.misc))
{
for(i=0;i<self.stopwatch.size;i++)
{
self.stopwatch[i] destroy();
self.stopwatch[i] = undefined;
if(i<2)
{
self.misc[i] destroy();
self.misc[i] = undefined;
}
}
self.stopwatch = undefined;
self.misc = undefined;
}
}
[/size]
Credit goes to CoDJumper.com modding forum.