I have been developing a pacman game using p5.js library. So i face the problem that sometimes the game was going slower,till almost get stopped. The solution i found was to add a clear() instruction in the last line of draw function.
But in most examples of code, i dont see this instruction used, so id ask if this is a good practice to reset and clear memory frequently, or is not recommended ? How should then use clear ? Is there any similar function.
If you have some interest ( i also have) i can passs you the code. The idea is that i loada 32x32 pixel image, with rock, points and pacman…and draw them in draw. I felt that sometimes specially after 30/40 seconds the game was beginning to slow down. Reading, i found the p5.clear() function ( at the last line of draw function, so every frame i reset memory), and i guessed that it cleaned cache and freeing memory. It worked.
So i was asking a more general queestion, if its used clear function and when. If it could clean some garbage and optimize the execution of scripts. But i didnt find any document or tutorial explainging the use of clear and if its a good practice.
@micuat: if you reset/clean the canvas, i suppose it also cleans or frees some memory where diferent elements of the draw function ( sprites, images, sounds…) are stored
So its any similar instruction when you face that some code is heavy to load, or the same code that works flawessly, in 30/40 seconds beginst to slow down, to the point of make it unplayable ?
If you have some interest ( i also have) i can passs you the code.
It’s more about sharing the code openly so others can have a better understanding - of course you don’t have to do so, but then we can only give you high level comments
i suppose it also cleans or frees some memory where diferent elements of the draw function ( sprites, images, sounds…) are stored
I don’t think so, looking at the code below. What you refer to is more about the data structure, and that’s something you have to handle by yourself.
So its any similar instruction when you face that some code is heavy to load
Again it’s hard to say anything without your code. Code optimization is not an easy task as there is no rule of thumb. And I doubt loading small images itself would cause huge performance drop unless you are loading a few gigabytes on the RAM that causes swap out.