Input from a Mouse Click

This program positions the ellipse with its centre at the position of the pointer when the mouse is clicked.

unit Unit1;

interface

uses
  SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game,
  SmartCL.GameApp, SmartCL.Graphics;

type
  TCanvasProject = class(TW3CustomGameApplication)
  public
    const MOB_WIDTH = 20;
    const MOB_HEIGHT = 15;
    MobX, MobY: integer;
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  MobX := 50;
  MobY := 100;
  GameView.OnMouseDown := procedure(o: TObject; b: TMouseButton; t: TShiftState; x, y: integer)
    begin
      MobX := x;
      MobY := y;
    end;
  GameView.Delay := 1;
  GameView.StartSession(True);
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  // Clear background with colour teal.
  Canvas.FillStyle := 'teal';
  Canvas.FillRect(0, 0, GameView.Width, GameView.Height);
  //Draw red ellipse.
  Canvas.FillStyle := 'red';
  Canvas.BeginPath;
  Canvas.Ellipse(MobX - MOB_WIDTH div 2, MobY - MOB_HEIGHT div 2, MobX + MOB_WIDTH div 2, MobY + MOB_HEIGHT div 2); //leftX, topY, rightX, bottomY
  Canvas.ClosePath;
  Canvas.Fill;
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

end.
   
Programming - a skill for life!

Input from the keyboard, mouse and touch using Smart Mobile Studio