Jump to content



Recommended Posts

Here goes a long one:
I'm making a game where I have some character sprites. Characters enter and leave the screen all the time, so I'm using a fixed number of sprites and just reutilize them to optimize performance.
When a character needs to appear, I pick one of the sprites outside the view and change it to be the new character.
All the characters have 3 animations, which are the same, lets say 'idle', 'walking' and 'jumping'. Of course, each characters animation has different frames.

So what I did was extend sprite class, and add 2 functions to it: one to initialize, adding the animations according to the character type of the sprite, and another one to free the sprite, using AnimationManager.destroy() so I can create new animations next time I need to use the sprite.
The free method would be somehow like this:


Character.prototype.free = function(){// Remove the sprite from the update loopthis.visible = false;this.exists = false;// Empty AnimationManagerthis.animations.destroy();}


The problem comes when the sprite is going to be reinitialized, I get this error message for every animaton I'm trying to add:
No FrameData available for Phaser.Animation idle
Checking Phaser source code, I see this is the reason:

destroy: function () {        this._anims = {};        this._frameData = null;        this._frameIndex = 0;        this.currentAnim = null;        this.currentFrame = null;    } 

_frameData is set to null.


Then, in AnimatoinManager.add method:


add: function (name, frames, frameRate, loop, useNumericIndex) {        if (this._frameData == null)        {            console.warn('No FrameData available for Phaser.Animation ' + name);            return;        }


It's obvious that trying to add an Animation to a AnimationManager after having used the destroy method will result in this error.
I've read the whole AnimationManager docs and code, and I only see this method to reset the frameData:

loadFrameData: function (frameData) {        this._frameData = frameData;        this.frame = 0;        this.isLoaded = true;    },

Which is meant to be a private method, and I'm not sure I should be calling this by any means.

So, summing up, am I doing something wrong calling AnimationManager.destroy? Should I do it any other way? Am I just missing anything to initialize the AnimationManager again?

Sorry about the huge post, but wanted to make everything clear.

Thanks in advance for your help :)

Link to comment
Share on other sites

Sorry to bump this, but I've been trying lots of things without succes. My last attempt was to fix it by calling Animation.destroy() on every animation, instead of AnimationManager.destroy(). Still not working.

Got really no idea how to empty the animations to be able to insert new ones with the same name.

Link to comment
Share on other sites

do you need to destroy the previous animation? does it not work to just add the new one?


My first attempt was to destroy the whole AnimationManager, as described in #1.


The second one was to simply try to overwrite the animations by using AnimationManager.add again without destroying anything, as you suggest. This resulted in totally inconsistent animations, that would start and stop at random frames.


The third approach was to destroy every animation by itself, then adding them again. This produced the same inconsistencies as simply overwriting the animations, which makes me guess that even removing the whole animation keeps some information about it in the AnimationManager somehow (couldn't find out).


Finally I 'solved' this by adding every possible animation for every sprite on create. Instead of 'idle' or 'walking', now I have spriteType+'idle', spriteType+'walking' and would play them according to the type.


I see this approach has some advantages over the previous one, since Animation objects are only created once at the begining of the game. I guess this must be less CPU consuming than constantly removing and creating new ones. On the other hand, if the number of sprite types and sprites on screen goes higher, the number of animations loaded will grow exponentially, which seems to be like a waste of memory to me.

Anyway, this is just theory, I don't know so much about the CPU or memory requirements of these operations, maybe Rich can shed some light on this.

Thanks for your help anyway :)

Link to comment
Share on other sites

  • 1 month later...

Rebumping this thread because I am porting this game to 1.2 and in this version creating hundreds of animations on game create is taking ages to load.

In 1.1.6 this step took quite a CPU load time, which already made me think it was not a very good practice, but now its taking ages.


How should I solve this problem? :( I think I need to just go back to the original idea and swap animations on the run, but still don't know how to do this with Phaser.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...