Cel :

Chcemy uzyskać efekt przesuwającej się piłeczki po pulpicie naszego programu, wzdłuż drogi poziomej.


Końcowy efekt :


Potrzebne komponenty :


Metoda :

1) Wstawiamy komponenty, wypisane w powyższej tabeli i zmieniamy im właściwość name na taką jaka jest w kolumnie "Nazwa"

2) Klikamy na puste pole formularza i w zakładce Events klikamy dwa razy na zdarzenie OnActive. Dopisujemy poniższy kod:

prawo := true;

3) W kodzie źródłowym znajdujemy napis " implementation ". Pod nim wpisujemy poniższy kod:

var
prawo : Boolean;

4) Do obsługi zdarzenia OnTimer komponentu Timer1 dodajemy kod:

procedure TForm1.Timer1Timer(Sender: TObject);
var
X : integer;
begin
if prawo = true then Kolo.left := kolo.left + 10;
if prawo = false then Kolo.left := kolo.left - 10;
if kolo.left = 0 then prawo := true;
X := form1.width - kolo.width - kolo.width;
if kolo.left >= X then prawo := false;
end;

5) Właściwość Interval komponent Timer1 ustawiamy na 100

6) Uruchamiamy program


Kod źródłowy :

unit Unit1;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;

type
TForm1 = class(TForm)
Kolo: TShape;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
prawo : Boolean;

procedure TForm1.Timer1Timer(Sender: TObject);
var
X : integer;
begin
if prawo = true then Kolo.left := kolo.left + 10;
if prawo = false then Kolo.left := kolo.left - 10;
if kolo.left = 0 then prawo := true;
X := form1.width - kolo.width - kolo.width;
if kolo.left >= X then prawo := false;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
prawo := true;
end;

end.