Input from a Mouse Move
This program positions an ellipse with its centre at the position of the pointer as the mouse is moved. The first version is coded with an anonymous procedure to handle the mouse move. You may prefer the second version, which assigns the procedure named MouseMove to GameView.onMouseMove. In each case, it is important that the parameter types match those expected. The second program positions an image with its centre at the position of the pointer as the mouse is moved. All of the code on this page compiles with Version 3.0 of Smart Mobile Studio.
unit Unit1; interface uses SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics; type TCanvasProject = class(TW3CustomGameApplication) public procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; end; const MOB_WIDTH = 20; MOB_HEIGHT = 15; var MobX, MobY: integer; implementation procedure TCanvasProject.ApplicationStarting; begin inherited; MobX := 50; MobY := 100; GameView.OnMouseMove := procedure(o : TObject; ss : TShiftState; x, y : integer) begin MobX := x; MobY := y; end; GameView.Delay := 1; GameView.StartSession(False); 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.
Code of Version with Named MouseMove Procedure
unit Unit1; interface uses SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics; type TCanvasProject = class(TW3CustomGameApplication) public procedure MouseMove(o : TObject; ss : TShiftState; x, y : integer); procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; end; const MOB_WIDTH = 20; MOB_HEIGHT = 15; var MobX, MobY : integer; implementation procedure TCanvasProject.MouseMove(o : TObject; ss : TShiftState; x, y : integer); begin MobX := x; MobY := y; end; procedure TCanvasProject.ApplicationStarting; begin inherited; MobX := 50; MobY := 100; GameView.OnMouseMove := MouseMove; 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.
Dragging an Image
You need to add this image named
koala.png to Resources so that it can be accessed by the code below.
Try a demo
of dragging the koala by mouse or touch on the next page.
unit Unit1; interface uses System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, SmartCL.Controls.Image; type TCanvasProject = class(TW3CustomGameApplication) private FImage : TW3Image; KoalaX, KoalaY : integer; protected procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; end; implementation procedure TCanvasProject.ApplicationStarting; begin inherited; FImage := TW3Image.Create(nil); FImage.LoadFromURL('res/koala.png'); GameView.OnMouseMove := procedure(o : TObject; ss : TShiftState; x, y : integer) begin KoalaX := x - FImage.Width div 2; KoalaY := y - FImage.Height div 2; end; GameView.Delay := 1; GameView.StartSession(True); end; procedure TCanvasProject.ApplicationClosing; begin GameView.EndSession; inherited; end; procedure TCanvasProject.PaintView(Canvas: TW3Canvas); begin // Clear background Canvas.FillStyle := 'white'; Canvas.FillRectF(0, 0, GameView.Width, GameView.Height); // Draw koala centred at pointer Canvas.DrawImageF(FImage.Handle, KoalaX, KoalaY); end; end.