PDA

View Full Version : How to move a list?



Wizard
08-03-2007, 08:28 AM
Hi everyone, I attach some of the code of my game written in delphi & delphiX. MyMen is a TList and consists of 19 men and they must move as per the TMan.DoMove procedure. How do I call the do move procedure in the playingStateMachine? If I use the SpriteEngine to move my road moves much too fast...

Maybe AthenaOfDelphi can help? :)

PS.: When I try to put the code in a block (code/pascal) some of it is cut off :evil:

procedure TMan.DoMove(MoveCount: Integer);
begin
if (fHit) then
begin
Movement := 0;
Y := Y + 1;
end
else
begin
if (isUp in form1.DXInput1.States) then
Y := y + 2 else
if not(isUp in form1.DXInput1.States) then
Y := Y + 1;
if y > Form1.DXDraw1.Height - 100 then
dead;
if (Movement = 0) Then
Movement := Random(2);
if X > 560 Then Movement := 1;
if X <50>=fRedLightStart) then
fPlayingState:=_psPlaying
else
renderPlay;
end;
_psPlaying : begin
if (RedLight<>nil) then
begin
redlight.Move(1);
if (RedLight.Deaded) then
begin
RedLight.free;

RedLight:=nil;

end;
end;

car.Move(1);
road.Move(1);

// Now move the obstacles

begin
dxSpriteEngine1.Move(myMen.Count); // .....???
end;

if (Car.deaded) then
begin
Car.free;

Car:=nil;

clearMen;

fGameState:=_gsEnd;
fEndState:=_esPlaySelected;

dxspriteEngine1.Items.clear;
end
else
renderPlay;
end;
end;
end;




MyMen := TList.create; is called in the OnFormCreate procedure.

cairnswm
08-03-2007, 08:55 AM
The idea is to call the TMan.move for each man so you need a list something like this

1. Create a loop for each item in the list
2. Call the DoMove for each item
2a. But TList store TObjects not TMyMan so you need to typecast the Object to a TMyMan first.


For I := 0 to MyMen.Count-1 do
Begin
TMyMan(MyMen[I]).DoMove(MoveCount);
End;

AthenaOfDelphi
08-03-2007, 09:07 AM
Hi Wizard,

William's beaten me to it by about 10 minutes :-)

With regards to the amount of code in a PASCAL tag... there is a limit on the amount of code you can put into a PASCAL tag in forum posts so they need to be short, sweet and to the point.

Wizard
08-03-2007, 09:16 AM
Thanks William 8)

Hi Athena, I thought as much :-)

I'm almost done with the new version of my game :-) Thanks for your help :-)

cairnswm
08-03-2007, 09:31 AM
Pleasure :)

But remember in DelphiX the Sprites and SpriteEngine do this for you. So by adding your TMyMan objects into the Sprite Engine they should move already.

From your example it seems you are not using the Sprite Engine (I also never did - could never get the sprites doing what I wanted).

AthenaOfDelphi
08-03-2007, 09:39 AM
He was using the sprite engine to do the movement originally, but in the code I sent him showing a bit more of an OOP way of doing it, I stopped using the sprite engines move handler directly... I can't remember why exactly... I think it was something to do with only being able to pass in a single value... I needed to pass multiple values.

Wizard
08-03-2007, 10:00 AM
Guys, I'm either very "dof" or there's something I'm not doing right. :oops:


if MyMen.Count > 0 then
for loop &#58;= MyMen.Count-1 downto 0 do
Begin
TMan&#40;MyMen&#91;loop&#93;&#41;.DoMove&#40;1&#41;;
end;

It does'nt work :evil:

AthenaOfDelphi
08-03-2007, 10:18 AM
Are you getting compiler errors or does it just not work within the game?

Wizard
08-03-2007, 10:25 AM
No compiler errors at all, the car and the road moves but the men doesn't even appear...

AthenaOfDelphi
08-03-2007, 11:25 AM
Is the code that creates the men the same as the example I sent you? If not, make sure the men are being linked to the sprite engine. Have a look at the example I sent you to check it (I passed a link to the sprite engine in the create method).

Failing that, send me the code and I'll have a look at it.

Wizard
08-03-2007, 11:57 AM
Dear Athena,

I tried to get it right but no joy, I'm sending my code to your e-mail address and will appreaciate it if you will have a look for me.

Thank you :-)

AthenaOfDelphi
08-03-2007, 12:15 PM
Hi Wizard,

You're gonna kick yourself I'm afraid.

There doesn't appear to be any faults with the man moving code, except an additional begin/end around the loop (commented out in the code below).


// begin
if (MyMen.count>0) then
for loop:=MyMen.count-1 downto 0 do
begin
TMan(MyMen[loop]).DoMove(1);
end;
// end;



Not that these make any difference, but they could confuse other stuff like syntax highlighters and such.

The problem actually stems from the creation. You have this code for creating the men.


Man:=TMan.create(30,-500,'man1',DXSpriteEngine1,DXImageList1);



I've made the same mistake you've made plenty of times and spent hours hunting for 'bugs'. What you actually need to do is, instead of allocating the newly created sprite to a variable, just add it to your MyMen TList. Like so.


MyMen.add(TMan.create(30,-500,'man1',DXSpriteEngine1,DXImageList1));



If you don't add them to the list, MyMen is empty when the code checks for man sprites and so they stay at their starting positions indefinitely :-)

Wizard
08-03-2007, 12:54 PM
Athena you are brilliant as allways :-)

I could'nt understand the use of MyMen in the loop to move because there was nothing assigned to MyMen...if it wasn't for you I probably would have spent hours on it too :-)

It works like a charm.....my little men and monsters have been released from the invissible world they were in :-)

Have a good day and thanks again :-)