Run a precedent while the component is pressed

Asked

Viewed 286 times

0

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

Is there any method or Event that is activated while the user keeps his finger by pressing the arrow button?

Follow 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 I care about:

UnitGame

As you can see now I use the Mousedown event, but as I said, it requires the user to keep clicking several times to run.

1 answer

1

Instead of moving in the event Mousedown, You can have a timer (or other similar process) to make the move. This timer is activated on Mousedown and disabled in Mouseup.

So the user presses the key, the timer turns on and starts to move 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 is a matter of adjusting the number of pixels to move and the interval of the timer to have the speed/fluidity you want, in this case I used timer with 30ms.

  • You can put an example?

  • edited with example

  • +1 I’ll see if it fits my problem tomorrow, but thank you already

Browser other questions tagged

You are not signed in. Login or sign up in order to post.