How to locate a object in a class pointer?

Started by
6 comments, last by FooFighter 20 years, 11 months ago
First, I''d like to thanks everyone who helpedme in the last topics! What I''d is to draw all the tiles in the TPainBox, so I did a Draw method in the TTile Class that handles all the drawing: procedure TTile.Draw; begin BackBuffer.Canvas.Draw(Self.X,Self.Y,Self.image); end; ... for i := 0 to NofTiles-1 do Tiles.Draw(); ... But that doesn seems to work. I used to work with Blitz Basic some time ago and in these situations I''d just do: for i = 0 to Each Tile ; Draw the current tile at its location EndFor What is the equivalent to this in Delphi?
Delphi just rules!!!
Advertisement
Okay.

Let me see If I can get this strait.

you have a class...
typeTTile = class(TObject)public   X,Y : integer;   image : TGraphic;   procedure Draw;end;procedure TTile.Draw;begin   BackBuffer.Canvas.Draw(Self.X,Self.Y,Self.image);end;//I''m assuming that you have something likevar Tiles : array [0..NofTiles-1] of TTile; //this could be dynamic// some where in a called fomr your draw loop.for i := 0 to NofTiles-1 do Tiles.Draw();MyPaintBox.Canvas.Draw(0,0,BackBuffer); //flip or equilvent 



If this is so then It all should be working okay.

To debug this, I''d start steping throught from the loop and see where this is running.
To be any more help, I''d need to see more code.

Sorry.

Armand
-------------------------
It is a good day to code.
Armand -------------------------It is a good day to code.
Hi Armand ,

I thought about creating an array of TTiles... but I'd like to know if its possible to cycle trough all the objects in the Tile pointer not in an array...

take this as an example and see if you undesrtand me:

var BadGuy : TBadGuy

...
for i := 0 to 10-1 do BadGuy := TBadGuy.Create;
...

So here would be created 10 TBadGuys "inside" the BadGuy var of type TBadGuy, am I correct?

So, if I want to cycle trough these objects and find a specific one, how to do that?

But I have another question here:

var Tile : array [1..Noftile-1] of TTile;

In this case, I'd have to create each tile object inside the array and each slot in the array 'd carry a pointer to a TTile object (and could point to many objects) or a object itself?

Thanks for the patience... apreciate your help a lot!



[edited by - FooFighter on April 29, 2003 9:45:26 PM]
Delphi just rules!!!
okay.

quote:
...
for i := 0 to 10-1 do BadGuy := TBadGuy.Create;
...

So here would be created 10 TBadGuys "inside" the BadGuy var of type TBadGuy, am I correct?


sorry, but no.
You''d have 1 TBadGuy, accessable by BadGuy and "lost" memory of the other 9 TBadGuy''s.

Perhaps I can answer your questions by tell''ing you the 2 ways I know of to do what you want, keeping with roughtly how your doing it.
There are lots of other ways to do stuff like this. (See TList or TObjectList).

Way 1. //static arrays
type   TTile = class(Tobject)   ..//stuff here.   end.const   c_TileCount = 10;var    g_Tiles : array[0..c_TileCount-1] of TTiles;//the initprocedure CreateTileSet();var i : integer;begin   for i := 0 to c_TileCount-1 do   begin      g_Tiles := TTile.create();      //load tile graphic and set x,y,...   end;end;procedure DrawAllTiles();var i : integer;begin   for i := 0 to c_TileCount-1 do   begin      g_Tiles.draw();<br>   end;<br>end;<br><br>procedure DeInitAllTiles();<br>var i : integer;<br>begin<br>   for i := 0 to c_TileCount-1 do<br>   begin<br>      g_Tiles.free;<br>   end;<br>end;<br> </pre> <br><br><br>Way 2. //dynamic arrays<br><pre><br>type<br>   TTile = class(Tobject)<br>   ..//stuff here.<br>   end.<br><br>var <br>   g_Tiles : array of TTiles;<br>   g_TileCount : integer;<br><br>//the init<br>procedure CreateTileSet(count : integer);<br>var i : integer;<br>begin<br>   g_TileCount := count;<br>   setlength(g_Tiles, count); //<br>   for i := 0 to g_TileCount-1 do<br>   begin<br>      g_Tiles := TTile.create();<br>      //load tile graphic and set x,y,…<br>   end;<br>end;<br><br>procedure DrawAllTiles();<br>var i : integer;<br>begin<br>   for i := 0 to g_TileCount-1 do<br>   begin<br>      g_Tiles.draw();<br>   end;<br>end;<br><br>procedure DeInitAllTiles();<br>var i : integer;<br>begin<br>   for i := 0 to g_TileCount-1 do<br>   begin<br>      g_Tiles.free;<br>   end;<br>   SetLength(g_Tiles, 0);<br>   g_TileCount := 0;<br>end;<br> </pre> <br><br><br>I hope this helps.<br>  </i>   <br><br>Armand <br>————————-<br>It is a good day to code.    
Armand -------------------------It is a good day to code.
You can also do this:


    TTile = class(TCollectionItem)private  FX, FY: Integer;  FImage: TBitmap;public  property X: Integer read FX write FX;  property Y: Integer read FY write FY;  property Image: TBitmap read FImage write FImage;end;TTiles = class(TCollection)private  function GetItem(Index: Integer): TTile;  procedure SetItem(Index: Integer; const Value: TTile);public  property Items[Index: Integer]: TTitle read GetItem write SetItem; default;end;function TTiles.GetItem(Index: Integer): TTile;begin  Result := TTile(inherited GetItem(Index));end;procedure TTiles.SetItem(Index: Integer; Value: TTile);begin  inherited SetItem(Index, Value);end;// Declare as private or public  MyTiles: TTiles;procedure TForm1.FormCreate(Sender: TObject);begin  MyTiles := TTiles.Create(TTile);end;procedure TForm1.FormClose(Sender: TObject);begin  MyTiles.Free;end;procedure TForm1.CreateTiles;var  I: Integer;  MyTile: TTile;begin  for I := 0 to 9 do  begin    MyTile := TTile.Create(MyTiles);    MyTile.X := I * 32;    MyTile.Y := I * 32;    MyTile.Image := TBitmap.Create;    MyTile.Image.LoadFromFile('C:\Images\Tile' + IntToStr(I) + '.bmp');  end;end;    


You can work with collections pretty much the same way you work with a TStringList.

[edited by - Harry Hunt on April 30, 2003 5:01:54 AM]
Armand: Thanks for the explanation and the code, very usefull, I did not know how to do such thing in Delphi (I used to code in BlitzBasic and now I know (read an article in blitzcoder) Blitz has an "ocult engine" to do such stuff so the "hard work" is done by Blitz and you can only do the "for n = 0 to Each TBadGuy" for example )

Harry: TCollectionItem seems to be a very powerfull way of doing this... thanks for the tip!

[edited by - FooFighter on April 30, 2003 11:01:34 AM]

[edited by - FooFighter on April 30, 2003 11:04:08 AM]
Delphi just rules!!!
Hey Harry!

Based on your tip above, I figured out an easier way of doing this :

I just used the TPictureCollection and TPictureCollectionItem classes that are included with DelphiX... so I would not need to implement a new TCollection n' TCollectionItem derived class, just had to declare two vars and create the objects:

Var
TileContainer : TPictureCollection;
Tile : TPictureCollectionItem;

...
for i := 0 to TileContainer.Count-1 do begin
Tile.Draw; end;
...




[edited by - FooFighter on April 30, 2003 5:48:25 PM]
Delphi just rules!!!
Woops... I was wrong, I did have to implement a new Class derived from TPictureCollection and TPictureCollectionItem becouse the TPictureCollectionItem class does not has the X and Y properies... And consequently I had to create a new class for TPictureCollection...

[edited by - FooFighter on April 30, 2003 7:39:14 PM]
Delphi just rules!!!

This topic is closed to new replies.

Advertisement