-3

I apologise for writing such a bad title, could you please edit it so that other people understand what i mean? i will explain:

I'd like to move a shape in a specific location, for example from x:=1000 to x:=600. But, if i write:

shape1.left:=600;

It will move the shape from 1000 (starting location) to 600, but i don't want it to jump 400mm , but first go to 992, then 184, then 176 etc, so that people can see that it is actually moving.

I tried using the command (sleep), but it didn't work, maybe it still jumps 400mm:

Shape1.Left:=1000;
sleep (50);
shape1.Left:=992;
sleep (50);
...
...

Thanks

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
KH Bill
  • 1
  • 1
  • 1

1 Answers1

5

Don't ever use Sleep in the main thread. It blocks your apps GUI thread which is a serious faux pas.

Instead drop a timer on the form. And write a timer handler like this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Shape1.Left := Shape1.Left - 5;
end;

The timer event handler is executed at regular intervals. Each time the timer ticks you update the position of the shape.

You will probably want to set the timer interval to be quite low so that the animation appears smooth. For example, try 100ms.

And you may also want to disable the timer when the shape reaches its target.

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Shape1.Left := Shape1.Left - 5;
  if Shape1.Left<=ShapeTargetLeft then
    Timer1.Enabled := False;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Well, it still moves the shape to -5, jumping so many coordinates, how do i make it slow down so that the user will be able to see that it is actually moving? – KH Bill Apr 17 '13 at 19:52
  • You mean: Shape1.left:= -1 Shape1.left:= -2 Shape1.left:= -3 Shape1.left:= -4 ....If you mean this...it is not working – KH Bill Apr 17 '13 at 20:00
  • 1
    No I don't mean that. I meant what I wrote. I added a sentence to explain a bit more. Perhaps you might read the documentation of `TTimer` to learn how they work. – David Heffernan Apr 17 '13 at 20:02
  • you are right, i am on it – KH Bill Apr 17 '13 at 20:08
  • 3
    I already sense another question coming "How do I stop a shape from moving in a timer?" – Jerry Dodge Apr 17 '13 at 23:28