Einzelnen Beitrag anzeigen

Benni0811

Registriert seit: 26. Sep 2016
25 Beiträge
 
Delphi 10.1 Berlin Starter
 
#3

AW: Probleme mit TTimer durch andere Anweisungen

  Alt 26. Jan 2017, 12:23
Die Tastensteuerung:
Delphi-Quellcode:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    case Key of
        vk_Left: begin
        Ship.Bild.Left:=Ship.Bild.Left-Pixel;
        if Ship.Bild.Left<0 then Ship.Bild.Left:=0;
        end;
        vk_Right: begin
        Ship.Bild.Left:=Ship.Bild.Left+Pixel;
        if Ship.Bild.Left+Ship.Bild.Width>Form1.Width then Ship.Bild.Left:=Form1.Width-Ship.Bild.Width;
        end;
        vk_Space: Ship.Shoot;
    end;
end;
Dieser Code stammt aus einem anderen Forum, anders hat es nicht funktioniert
Delphi-Quellcode:
type
  TForm1 = class(TForm)
  private
    procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
  public
    { Public-Deklarationen }
  end;

...

procedure TForm1.CMDialogKey(var Message: TCMDialogKey);
begin
  with message do begin
    case charcode of
     vk_Left: begin
        Ship.Bild.Left:=Ship.Bild.Left-Pixel;
        if Ship.Bild.Left<0 then Ship.Bild.Left:=0;
    end;
        vk_Right: begin
        Ship.Bild.Left:=Ship.Bild.Left+Pixel;
        if Ship.Bild.Left+Ship.Bild.Width>Form1.Width-17 then Ship.Bild.Left:=Form1.Width-Ship.Bild.Width-17;
    end;
    vk_Space: Ship.Shoot;
    else
      inherited;
    end;
  end;
end;
Der Timer:

Delphi-Quellcode:
Procedure TForm1.ShotTimer(Sender: TObject);
begin
    if Sender=Ship.Shot.Timer then begin
        Ship.Shot.Bild.Top:=Ship.Shot.Bild.Top-Pixel;
        if Ship.Shot.Bild.Top+Ship.Shot.Bild.Height<=0 then Ship.Hit;
    end;
end;
TShot ist eine eigene Klasse mit eingebautem Timer, der hier erstellt wird:
Delphi-Quellcode:
Constructor TShot.Create(X,Y,W,H: Integer; AP: TForm);
begin
    inherited Create(x,y,w,h,ap);
    Timer:=TTimer.Create(AP);
    with Timer do begin
        Interval:=50;
        OnTimer:=Form1.ShotTimer;
        Enabled:=True;
    end;
end;
  Mit Zitat antworten Zitat