Wir sollten mal die Schärfe rausnehmen, und uns um das Problem kümmern.

Zitat von
just.cause:
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var d:real;
begin
d:=sqrt((shape1.Left - shape2.Left)*(shape1.Left - shape2.Left)+(shape1.Top - shape2.top)*(shape1.Top - shape2.top));
if (d>=shape1.width) or (d>=shape1.height) then showmessage ('tagged!');
end;
end.
Ist so ziemlich unsinnig. Warum solltest Du beim Programmstart auf Kollision prüfen.
Das gehört woanders hin.
Delphi-Quellcode:
procedure TForm1.keypress(Sender: TObject; var Key: Char);
var d:real;
begin
d:=sqrt((shape1.Left - shape2.Left)*(shape1.Left - shape2.Left)+(shape1.Top - shape2.top)*(shape1.Top - shape2.top));
if (key ='s') and (d>=shape1.width) then shape1.top:=shape1.top +10;
if (key ='w') and (d>=shape1.Width) then shape1.top:=shape1.top -10;
if (key ='d') and (d>=shape1.Height) then shape1.Left:=shape1.Left +10;
if (key ='a') and (d>=shape1.Height) then shape1.left:=shape1.left -10;
label1.Caption:=inttostr(shape1.top);
label2.Caption:=inttostr(shape1.Left);
if (key ='k') and (d>=shape1.width)then shape2.top:=shape2.top +10;
if (key ='i') and (d>=shape1.width)then shape2.top:=shape2.top -10;
if (key ='l') and (d>=shape1.Height)then shape2.Left:=shape2.Left +10;
if (key ='j') and (d>=shape1.Height)then shape2.left:=shape2.left -10;
label3.Caption:=inttostr(shape2.top);
label4.Caption:=inttostr(shape2.Left);
end;
Ist schon sehr schön, aber ich würde das mit einer case Anweisung machen. Ist aber Geschmackssache.
Den Code zur Berechnung des Abstandes würde ich aber in eine Funktion auslagern, da du ihn für die Kollisionsprüfung nochmal brauchst.
also: im private Abschnitt die Funktion deklarieren, und dann im Code Bereich erstellen.
Delphi-Quellcode:
...
private
function CalcAbstand: real;
...
Implementation
....
function TForm1.CalcAbstand: real;
begin
result := sqrt((shape1.Left - shape2.Left)*(shape1.Left - shape2.Left)
+ (shape1.Top - shape2.top)*(shape1.Top - shape2.top));
end;
jetzt kannst Du die in deiner keypressed prozedur verwenden:
Delphi-Quellcode:
procedure TForm1.keypress(Sender: TObject; var Key: Char);
var d:real;
begin
d := CalcAbstand;
...
end;
und die Kollisionsprüfung gehört an's Ende der keypressed Prozedur erneut hin.
Allerdings ist deine Prüfung genau verkehrt herum.
Delphi-Quellcode:
...
d := CalcAbstand;
if (d < shape1.width) or (d < shape1.height) then // hier die Bedingung richtig schreiben
showmessage('tagged!');
end;
Dann sollte es klappen.