BdR Posted October 15, 2014 Share Posted October 15, 2014 I have a group of enemy sprites, this is a pool of sprites that is recycled by kill() and revive(). They are reused, so at one point it may be enemy type A which is killed and then that same sprite is revived but now it is enemy of type B. This is done by setting the animation at the moment of reviving an enemy. So I use the same animation name every time and I assume it is overwritten, in other words the old animation of the previous enemy is not kept in memory. Is that correct? My github example project is updated with crudely animated sprites (it's just an example), see updated github source here:https://github.com/BdR76/phaseranimals This is the part of the code that creates or revives an enemy sprite:function createAnimal(antype) { .. // get inactive animal from animals object pool var animal = this.animalsGroup.getFirstDead(); // if there aren't any available, create a new one if (animal === null) { animal = new Animal(game, x, y, antype); this.animalsGroup.add(animal); }; animal.animations.add('myanimation', [ antype, antype+5, antype+10, antype+5], 8, true); animal.play('myanimation', 5, true, false); // fps=5, loop=true, killOnComplete=false // else revive the animal (set it's alive property to true) animal.revive(); animal.x = x; animal.y = y; //etc.So my question is, can I reuse sprites and overwrite their animation every time it is revived, without any problems? I mean, it *seems* to work but won't there in the long run be some sort of memory leak? Link to comment Share on other sites More sharing options...
lewster32 Posted October 16, 2014 Share Posted October 16, 2014 I'd say add all the animations to each sprite on creation, and then just play the relevant animation when you revive it; takes the guesswork out of it. Link to comment Share on other sites More sharing options...
BdR Posted October 17, 2014 Author Share Posted October 17, 2014 Good point. At first I thought it would cache the frame canvases for each sprite taking too much memory. But looking at the code an animation is nothing more than some frame indexs (arrays of ints) so I'll just add all animations to all sprites. Thanks lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts