PDA

View Full Version : Counting down with delphix's timer..



Wizard
22-02-2007, 10:13 AM
Hi all, I have the following procedure in my game which is called in the DXTimer event. The car should stop when the gas runs out... Problem is that when the app runs the car immediately stops and the game-end procedure follows...

Maybe the DXtimer is too fast? Help please :-)


procedure TForm1.RemainingGas;
var GasLevil, FullTank : integer;
begin
FullTank := 100;
for GasLevil := FullTank downto 0 do
begin
if &#40;GasLevil <= 0&#41; then
car.Moved &#58;= false;
end;
end;

AthenaOfDelphi
22-02-2007, 10:36 AM
Does car.moved:=false tell the rest of the game that the car is out of gas?

If so, your car will stop on the first run through this procedure because the for loop will execute and set car.moved to false on the last run through its loop.

How many cars are there?

I would put the cars gas level in the car object. And then in your 'remainingGas' routine do something like this...


car.moved:=(car.gas>0);


Assuming as well that you want to decrease the fuel, in that routine, then this is more appropriate:-


car.gas:=car.gas-1;
car.moved:=(car.gas>0);


Unfortunately its a bit difficult without seeing all the code to comment any further. But even that second block of code is likely to result in early termination as it would take less than 2 seconds to use 100 units of gas (assuming a timer frequency equivalent to 60fps).

To get around that you can either use real numbers and subtract a fraction every cycle or you can use integers and give the car say 1000 units of gas and only subtract 1.

Hope this helps... if not, then I could do with seeing more of the code.

FNX
22-02-2007, 11:11 AM
Hi, what Athena said is correct, if you do a for loop the car's fuel will run
out in just one step.

The DXTimer itself is your for loop :)

Also, i can't understand why using a boolean also (.Moved) as you
can just test the remaining gas into the "TCarSprite.DoMove" having
if (car.gas>0) then do all your car move routine

Gambare! ;)

Wizard
22-02-2007, 12:12 PM
Thanks for your help :-)

It now works as I want it to :-)

Global variable : gas as integer, gas := 1000 in my startmain procedure and the following:


procedure TForm1.RemainingGas;
begin
Gas &#58;= Gas -1;
car.Moved &#58;= &#40;gas>0&#41;;
end;

The for loop is so attractive but I understand that the timer is the for loop :-)

Thanks again, you guys/girls are brilliant!