#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,055
    Rep Power
    383
    As far as I can see, in the code you posted, you never actually create a car object.

    Comments on this post

    • bennywilks agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,055
    Rep Power
    383
    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

IMN logo majestic logo threadwatch logo seochat tools logo