CreateSprite

Just starting out? Need help? Post your questions and find answers here.
User avatar
_aNdy_
User
User
Posts: 41
Joined: Fri Jun 17, 2016 12:06 am
Contact:

CreateSprite

Post by _aNdy_ »

I'm coding my first full game (after a couple of false starts) after having already coded a few applications. I'm creating a sprite with:

Code: Select all

Global sp_LEVELMAZE = CreateSprite(#PB_Any, #TILEW * #MAZEW, #TILEH * #MAZEH)
If the code repeats and then this command is encountered again, does 'CreateSprite' delete the old version and create a new one, or is the 'old' sprite floating in memory somewhere needing to be 'freed'?
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: CreateSprite

Post by jacdelad »

By using #PB_Any the function returns a handle. It's up to you to delete the sprite, because next time another handle is returned and the current one still stays valid (though your variable now points to the new handle).
It's differently, when not using #PB_Any: If the handle with a fixed number is already in use, it is freed and recreated with the new parameters by the function.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: CreateSprite

Post by spikey »

In this instance, because you are using #PB_Any, the old one will still be around but you'll no longer have a 'handle' to it because you've overwritten the value of sp_LEVELMAZE.

You can use IsSprite and FreeSprite to be tidier:

Code: Select all

Global sp_LEVELMAZE 
If IsSprite(sp_LEVELMAZE)
  FreeSprite(sp_LEVELMAZE)
EndIf
sp_LEVELMAZE = CreateSprite(#PB_Any, #TILEW * #MAZEW, #TILEH * #MAZEH)
User avatar
_aNdy_
User
User
Posts: 41
Joined: Fri Jun 17, 2016 12:06 am
Contact:

Re: CreateSprite

Post by _aNdy_ »

Thanks for the quick responses! All understood and code inserted!
Post Reply