The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
OOP - Image Movement
Discuss OOP - Image Movement in the Delphi Programming forum on Dev Shed. OOP - Image Movement Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 8th, 2012, 04:29 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 56 m 22 sec
Reputation Power: 0
|
|
|
OOP - Image Movement
I am just starting to learn delphi, in particular in school we have been doing Classes. I thought i'd give it a go trying to make an object move around using the arrow keys.
So Far this is my Class Definitions.
"unit ClassDefs;
interface
uses
Classes, StdCtrls, Buttons;
type
TCar = Class
Private
inX : Integer;
inY : Integer;
Public
Procedure UpKey;
Procedure DownKey;
Procedure LeftKey;
Procedure RightKey;
Function XPosition : Integer;
Function YPosition : Integer;
Function GetinX : Integer;
Function GetinY : Integer;
End;
implementation
Function TCar.XPosition;
begin
Result := inX;
end;
Function TCar.YPosition;
begin
Result := inY;
end;
Procedure TCar.UpKey;
begin
inY := inY + 1;
end;
Procedure TCar.DownKey;
begin
inY := inY - 1;
end;
Procedure TCar.LeftKey;
begin
inX := inX - 1;
end;
Procedure TCar.RightKey;
begin
inX := inX + 1;
end;
Function TCar.GetinX : Integer;
begin
Result := inX;
end;
Function TCar.GetinY : Integer;
begin
Result := inY;
end;
end."
And then this is my main Program.
"unit RaceUnit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, ClassDefs , Vcl.Dialogs, Vcl.ExtCtrls,
Vcl.Imaging.pngimage;
type
TfrmRace = class(TForm)
imgRacecar: TImage;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
public
end;
var
frmRace: TfrmRace;
Car : TCar;
implementation
{$R *.dfm}
procedure TfrmRace.FormCreate(Sender: TObject);
begin
imgRacecar.Left := 10;
imgRacecar.Top := 10;
end;
procedure TfrmRace.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_UP : Car.UpKey;
VK_DOWN : Car.DownKey;
VK_LEFT : Car.LeftKey;
VK_RIGHT : Car.RightKey;
end;
imgRacecar.Top := Car.XPosition;
imgRacecar.Left := Car.YPosition;
end;
end."
So far I have managed to get the movement working without Using Classes, but whenever I put it into a Class it doesn't seem to work. Now after playing around with it for an hour or so i've decided to rest and seek Help.
|

October 8th, 2012, 05:42 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
As far as I can see, in the code you posted, you never actually create a car object.
|

October 9th, 2012, 04:14 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 56 m 22 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by clivew As far as I can see, in the code you posted, you never actually create a car object. |
Thanks, just as a further question. Do you know how i would create the Car Object as an Image Class.
|

October 9th, 2012, 03:42 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
There are a number of different approaches you could take.
One as you suggest, inheriting from TImage.
Another would be passing a pointer to TImage inside TCar (but that is for another day).
Taking your original question and (with no testing) hopefully getting it right
Something like this:
Code:
type
TCar = class(TImage)
Public
Procedure MoveUp;
Procedure MoveDown;
Procedure MoveLeft;
Procedure MoveRight;
end;
implementation
Procedure TCar.MoveUp;
begin
Top := Top -1;
end;
Procedure TCar.MoveDown;
begin
Top := Top + 1;
end;
Procedure TCar.MoveLeft;
begin
Left := Left -1;
end;
Procedure TCar.MoveRight;
begin
Left := Left + 1;
end;
end.
Clive
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|