Execute a procedure while the component is pressed

0

I have a multi-device application made in Delphi-XE8, in it I have a rocket image ( TImage ) and other 2 arrows, I would like the rocket to move on the X axis of the screen (% ), but with the current code the user would have to press several times to change the position of that component.

Is there any method or event that is activated while the user is holding the finger by pressing the arrow button?

Here is the code I currently have:

unit Game;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.ExtCtrls, FMX.Objects;

type
  TForm1 = class(TForm)
    ShipImage: TImage;
    ImageRightArrow: TImage;
    ImageLeftArrow: TImage;
    procedure ImageLeftArrowMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    procedure ImageRightArrowMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}




procedure TForm1.ImageRightArrowMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
begin
   ShipImage.Position.X := (ShipImage.Position.X + 10);

end;

procedure TForm1.ImageLeftArrowMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
begin
   ShipImage.Position.X := (ShipImage.Position.X - 10);
end;

end.

And an image of the part of Unit that matters to me:

As you can see now I use the MouseDown event, however as I said, it requires the user to click several times to run.

    
asked by anonymous 08.12.2017 / 19:33

1 answer

1

Instead of moving in the MouseDown event, you can have a timer (or similar process) to make the move. This timer is enabled on MouseDown and disabled on MouseUp .

As soon as the user presses the key, the timer switches on and starts moving the object until the user removes his finger and the timer is stopped.

Example:

var fDirection:integer;

procedure TForm2.ImgLeftMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Single);
begin
   //Vai para a esquerda
   fDirection:=-1;
   //liga  o timer
   TmrMovimento.Enabled:=true;
end;

procedure TForm2.ImgLeftMouseUp(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Single);
begin
   //Pára  o timer
   TmrMovimento.Enabled:=false;
end;

procedure TForm2.ImgRightMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Single);
begin
  //Vai para a direita
  fDirection:=1;
  //liga  o timer
  TmrMovimento.Enabled:=true;
end;

procedure TForm2.ImgRightMouseUp(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Single);
begin
   //Pára  o timer
   TmrMovimento.Enabled:=false;
end;

procedure TForm2.TmrMovimentoTimer(Sender: TObject);
begin
  //Move para a direçao pretendida
  ShipImage.Position.X := (ShipImage.Position.X + (10*fDirection));
end;

Now it's a matter of adjusting the number of pixels to move and the interval of the timer to have the speed / fluency you want, in this case I used 30 ms timer.

    
09.12.2017 / 00:12