Reputation: 1
I want to animate a token moving across the board X number of times.
In this case, X will be the number rolled on a die.
For example, if a player rolls a 3, I want to see the token "move" to the next tile, and then 400ms later to the next tile, then 400ms later to the last tile.
I have tried using a TTimer
, which animates this well, but it does not stop the token on the right tile. Using a for
loop results in the token ending up on the right tile, but without animation. I just want the TTimer
to repeat itself X number of times. :)
My code is as follows (using Delphi 2010):
For the For Loop:
for i := 1 to iNum + 1 do // iNum is the number rolled
player1.Left := player1.Left + 200; // player1 is the token`
For the TTimer:
procedure TfrmSnakesNLadders.tmrMoveTimer(Sender: TObject);
begin
player1.Left := player1.Left + 200;
if player1.Left >= 850 then // 850 is the Rightmost Boundary of the token
tmrMove.Enabled := false;
end;
Upvotes: 0
Views: 140
Reputation: 6013
You don't show us how you initialise player1.right, but it looks like a boundary condition error, i.e. your 850 value should not be 850. But to roll x times, initialise a class variable and count that. A bit like this:
class TfrmSnakesNLadders = class( Form )
….
private
fNum : integer
….
Then initialise and start your timer, something like
….
procedure TfrmSnakesNLadders.InitMove;
begin
fNum := 0;
tmrMove.Enabled := true;
end;
and then animate with your timer
procedure TfrmSnakesNLadders.tmrMoveTimer(Sender: TObject);
begin
player1.Left := player1.Left + 200;
inc( fNum);
if fNum >= iNum then
tmrMove.Enabled := false;
end;
Upvotes: 1