Page 1 of 1
CreateSprite
Posted: Fri Jan 19, 2024 1:21 pm
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'?
Re: CreateSprite
Posted: Fri Jan 19, 2024 1:32 pm
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.
Re: CreateSprite
Posted: Fri Jan 19, 2024 1:38 pm
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)
Re: CreateSprite
Posted: Fri Jan 19, 2024 2:10 pm
by _aNdy_
Thanks for the quick responses! All understood and code inserted!