Delphi - Poruszanie się TImage

0

Witam!
Mam problem.
Właściwie nie wiem, co się stało, najpierw działało, a potem przestało.

Chciałem zrobić tak, aby TImage1 mogło poruszać się w prawo i w lewo, lecz do pewnego punktu.
Zrobiłem zmienne lewo i prawo (Boolean).

 procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
 begin
  if Image1.Left=0 then lewo:=false
  else lewo:=true;
  if (Key = VK_LEFT) and (lewo=true) then Image1.left := Image1.left - 5;
  
if Image1.Left=320 then prawo:=false
  else prawo:=true;
  if (Key = VK_RIGHT) and (prawo=true) then Image1.left := Image1.left + 5;
 end;

Jednak gdy włączam program, obrazek nie zatrzymuje się.
Proszę o pomoc, ewentualnie o podanie innego pomysłu zatrzymania się.

PS: W Delphi jestem słaby, jak na razie mój najlepszy program to kalkulator, więc jeśli powód jest bardzo prosty, proszę się nie śmiać. :)

0

bo jak Image.Left = 3 i odejmiesz od tego 5 to wyjdzie -2.
Te Twoje warunki to tak trochę nie tewges. Całość można zapisać tak

if (Key = VK_LEFT) and (Image1.Left > 0) then
  Image1.Left := Image1.Left - 5;

analogicznie w prawo

0

Dzięki, teraz działa :)

0
TKeys = record
  kLeft, kRight: Boolean;
end;

var
  Keys: TKeys;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Keys.kLeft then Image1.Left := Image1.Left - 5;
  if Keys.kRight then Image1.Left := Image1.Left + 5;
end;

procedure TForm1.FormKeyDownUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Keys.kLeft := (Char(Key) = 'A');
  Keys.kRight := (Char(Key) = 'D');
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1