Archived

This topic is now archived and is closed to further replies.

DelphiX - sprite engine

This topic is 6169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have an array of TImageSprite objects. They are displayed with a SpriteEngine. If I delete an element of the array AllBullets := nil; or AllBullets[i].destroy; They deleted elements are still displayed at new frames if I call SpriteEngine.Move... I want to dynamically load and unload the elements. In my game, the elements of the TImageSprite are bullets... array[0..255] of TImageSprite; please help CU DOuglas

Share this post


Link to post
Share on other sites
Hi Douglas,

Use Tlists instead, they''re easier to manage.

however, if you prefer arrays, you have to free your item number not destroy it.

soo

type

TBulletsprite = class(TimageSprite)
.
.
private
Bullets : array[0..50] of TBulletsprite;
.
.
in your form create
bullet[itemnumber] := TBulletsprite.create(dxengine1.engine);

to free them
bullet[itemnumber].free;

now, if you want to dynamicaly assign new data to your bulletsprite, you don''t need to delete it, just reassing it some new propreties.

bullet[itemnumber].image:= imagelist1.items[newimage];
bullet[itemnumber].width :=imagelist1.items[newimage].width;
bullet[itemnumber].height:=imagelist1.items[newimage].height;
bullet[itemnumber].visible:= true;

... etc

remember, if a sprite''s visible proprety is set to false, the engine ignores it.

If you plan on using dynamic arrays, make sure you don''t resize the array during gameplay.
Build your level, create the number of items you will need for that level.. play the level with those.
resizing dynamic arrays during gameplay will give you big performace hits.
If you don''t resize them, the preformance is comparable to regular arrays and TLists

Hope this helps a bit.

Gunner.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thank you Gunner!

Your reply helped me indirectly.

I had the AllBullets.free method call in the routine for collision detection with the other tank.


But of course, it also has to be in the routine for reaching the limits of the screen.

(I wondered why they become invisible after hitting the enemy tank...)

Thanx so much!

CU Douglas

Share this post


Link to post
Share on other sites
Thank you Gunner!

Your reply helped me indirectly.

I had the AllBullets.free method call in the routine for collision detection with the other tank.


But of course, it also has to be in the routine for reaching the limits of the screen.

(I wondered why they become invisible after hitting the enemy tank...)

Thanx so much!

CU Douglas

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for i := 0 to 255 do
begin
if AllBullets <> nil then
begin
AllBullets[i].X := AllBullets[i].X + Cos256(AllBullets[i].Angle) * 4;
AllBullets[i].Y := AllBullets[i].Y + Sin256(AllBullets[i].Angle) * 4;

if AllBullets[i] <> nil then
if AllBullets[i].X < 0 then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].Y < 0 then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].X > Screen.Width - AllBullets[i].Width then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].Y > Screen.Height - AllBullets[i].Height then AllBullets[i].Free;
}
end;
end;

-----------------------------------------------------------

I want to remove an element from the array if it reached the borders of the screen.
But I always get an EAccessViolation!

Where is my fault? Pleas tell me, coz I already tried a lot.

CU Douglas

Share this post


Link to post
Share on other sites
for i := 0 to 255 do
begin
if AllBullets <> nil then
begin
AllBullets[i].X := AllBullets[i].X + Cos256(AllBullets[i].Angle) * 4;
AllBullets[i].Y := AllBullets[i].Y + Sin256(AllBullets[i].Angle) * 4;

if AllBullets[i] <> nil then
if AllBullets[i].X < 0 then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].Y < 0 then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].X > Screen.Width - AllBullets[i].Width then AllBullets[i].Free;

if AllBullets[i] <> nil then
if AllBullets[i].Y > Screen.Height - AllBullets[i].Height then AllBullets[i].Free;
}
end;
end;


------------------------------------------------

I wanna remove the elements from the array if the element reaches
the border of the screen.
But I always get an EAccessViolation!
Please tell me! I already tried a lot!

CU Douglas

Share this post


Link to post
Share on other sites
Ok you want your sprites to die after a condition?
In TYourSprite

procedure TYourSprite.DoMove(MoveCount: Integer);
begin
if DieCondition then
Dead;
end;


Then in DXtimerOnTimer
...

DXSpriteEngine.Move(Timedelta);
DXSpriteEngine.Dead;

...

What this does :
When a sprite dies it doesnt actually get freed till DXSpriteEngine.Dead is called. This prevents the access violation, as the sprite is cleanly removed from the DXspriteEngine

Share this post


Link to post
Share on other sites