Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Is it better to delete sprites when not being used?

Author
Message
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 12th Jul 2022 02:37
Is it better to delete sprites and images of lets say, for example, menus in a game and reload the images and create the sprites when that menu is called on? To take it further things like gun bullets should they also be deleted when off screen or hit the targets? Or is it better to have a bunch "preloaded and made" ready to just be repositioned ? This is in regards toy 2d game btw and thanks in advance.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 12th Jul 2022 03:39 Edited at: 12th Jul 2022 03:43
Quote: "Is it better to delete sprites and images"

i pre-load all images i may need and never delete them.

when it comes to bullet sprites (assuming lots), i always delete them on impact but i may not delete them if they leave the screen.

assuming "a lot" of bullets as in Liberator, initially i let the bullets leave the screen (where the map is larger than the screen) and didn't delete them until they hit something or left the map. for game balance purposes, i now delete them if they leave the screen since it was too easy to fire blindly and hit enemies that i couldn't see.

but, at some point i do always delete them while others would suggest moving them out of view, setting them invisible, etc,, and re-using them but i personally see no performance hit of note in deleting and re-creating them on demand.

liberator handles it all pretty well, even under HTML export (maintaining ~30FPS, and a solid 60 on WIN which i'm sure could do hundreds if i let it) and you see i probably have more sprites doing other things than i do bullets. ie, the tank tracks, explosions, decals, etc, all sprites (while the tanks and bullets and a few physics obstacles are all that move). speaking of tanks, i DO re-use those (immediately) where, on kill, i simply reposition them. but, again, it's just easier for me to re-use them vs recreating, adding physics back onto them, etc.

the current project i'm working on has slightly more than i've worked with before but still minimal Menu items. in trying to be efficient, i still always pre-load any images i might need then generate the sprites on demand and .insert their IDs into a MenuArray[]. when i close the menu, the last thing i do is use the array for reference and delete all sprites generated for the menu. some of that is for efficiency, the rest is simplicity in "closing" a menu.

again, others may suggest to move sprites off screen and re-use them but i think you and i are safe in deleting and re-creating where our media is relatively quite small compared to AAA releases while AppGameKit has our back in handling sprites pretty well.
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 12th Jul 2022 13:42
Thank you for the detailed answer. I guess I was trying to see if deleting the bullet and re-creating the sprite would be more or less effort then just repositioning it.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Jul 2022 17:32
I will estimate how many bullets I expect can be on the screen at one time, then make a little more. I'll use a routine that cycles through them and just hide what's not active. If its sprites you won't use anymore (level specific sprites, background images), you can delete those upon level completion as you load the next. It all really depends on the structure of your game.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 26th Jul 2022 22:09
Hello dandrew

I look at it this way either way at some stage you need to recreate the sprites and maintain them il refer to that as housekeeping
reuse of code is one reason programmers may sometimes look for other approaches

heres a checklist

1> Will I need that sprite again (this includes the actual identification that goes with the ID)
2> Is there only one image needed for that same sprite and is that image also needed (so the id needs keeping to for the image and someway of the sprite id perhaps)

Constants are great for creating a sprite assigning it the same id where you dont and wont get another id created the same causing conflicts later down the track
i.e createsprite(1,idimage) will create a sprite with a constant of one that's great sometimes for lets say you have your main spaceship this means deleting it and recreating can work fine

now bullets for example you might have shot 100 bullets and there may be others who have shot 100 bullets this is where deleting is a little trickier

what I do in that case is use an array to stored the identification of bullets, images and there locations sometimes because the next time you use bullets there may be 50 there may be 150 constants are nolonger appropriate

I define types
i.e

then you can remove from the arrays as you delete the bullets

not sure if youve used commands like abullet.insert.(something) but can also remove at the same time



PK

Login to post a reply

Server time is: 2024-04-23 22:11:02
Your offset time is: 2024-04-23 22:11:02