Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 8th, 2012, 04:29 PM
bennywilks bennywilks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 bennywilks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old October 8th, 2012, 05:42 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
As far as I can see, in the code you posted, you never actually create a car object.
Comments on this post
bennywilks agrees!

Reply With Quote
  #3  
Old October 9th, 2012, 04:14 AM
bennywilks bennywilks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 bennywilks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 9th, 2012, 03:42 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
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
Comments on this post
bennywilks agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > OOP - Image Movement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap