How to make 100 Bunnies Hop while accessing a single animation action

I am fairy new to Cocos2dx and I am trying to wrap my mind around how animations are performed. I am trying to optimize performance by having as little overhead as possible with an abstracted class.

Let’s say I wanted to make 100 bunnies that have a hopping animation among other animations. I have a Bunny class and a MetaBunny class. The MetaBunny class would ideally perform as much of the animation preparation as possible so that all a bunny has to do is simply:

this->runAction(metaBunny->animations.hop);

where animations is a struct with member hop which returns something to the effect of:

RepeatForever::create(Animate::create(Animation::createWithSpriteFrames(animationFrames, animationSpeed)))

I’ve tried this and it seems that only 1 bunny can access the hopping at once. Strangely it hops much faster, as if the animations of all the bunnies are being channeled into the last bunny. Let’s call it the super bunny. I’ve manually had some of the other bunnies perform other animations from the struct and the superbunny no longer has access to those animations. It seems that only 1 bunny can access the animation at once.

So I have a few questions from all of this:

  1. What portion of the animation process must I put in the instanced bunnies and what can (or ought) go in the metaBunny?
  2. Is my desire to house common variables in a metaClass object against best practices for Cocos2dx, or C++? If so, what should I be doing instead so that I maximize performance and abstraction in helpful ways?
  3. If each bunny must have its own set of variables (say animation frames) does this mean that the sprite sheet is loaded multiple times as well?
  4. I am not using TexturePacker, d you have any recommendations on resources to better understand how to work with animation frames and sprites in Cocos2dx? Most of what I have found seems outdated.

Thank you for your time looking at this. :rabbit2:

if you want to use an action more than once make sure you are calling action->clone() for each time you want to re-use it.

1 Like

Thank you for your reply. This definitely fixed the issue I was having.

However I am wondering if I am actually saving any processing by doing this. Does the sprite sheet get cloned too for every animation and every instance of that animation, or is it just the frames that are cloned, or just the references to the frames?

If the instance class is just cloning data from the meta class on use, wouldn’t it be better to localize the animation data in each instance if is being called often enough? In other words, the meta class serves as a constructor rather than a resource that houses common variables between instances.

Ultimately I am trying to wrap my mind around what the animation data is and what can be abstracted in a meta class and what needs to be contained or in this case, cloned into the instance class.

Thanks!

The spritesheet is cached.

Actions control the state of an animation. If you want your bunnies to hop separately you need to instance Animate for each of them

SpriteFrames abstract the texture and UV coordinates required to display a frame. Internally they reference a texture, loaded via the TextureCache when SpriteFrame::create() is used.

If you wish to cache your animations to avoid rebuilding them for every bunny, I would recommend using AnimationCache.

I don’t think there’s really a need for your meta class. Animations could be loaded to cache, then obtained by name when building a bunny.

1 Like

If you want 100+ clones of anything you will probably want to write your own system using cocos2d-x components. Anything beyond these numbers really should be done in a different way than the standard cocos2d-x patterns. Think tilemaps, particle systems, etc. You’ll probably want to animate by using a single Animation and changing sprite frames directly instead of using multiple instances of the same action(s) and animation(s). IMHO

Do you mean like a manager class, like a Flyweight style design pattern?

Mostly just a reminder that cocos2d-x doesn’t have to be used strictly with Sprites and Actions only. Also, the questioner asked about doing something more like the particle system so I guess I’m just letting them know that they don’t have to follow the strict scene-graph-action structure of the generic cocos2d-x pattern.

As for some specifics I would probably just have a Bunny/System/BunnyManager/etc class (or whatever you want to call it) that references the texture and an array of unique instance properties like position, cur frame (and other animation properties), and tint color or whatever. Update all the unique properties using code from Animate action alongside the game logic and then just render them all in visit with TriangleCommand (copy from Sprite or ParticleSystem classes).

It’s up to the developer(s) to decide which they prefer and then whether or not there may be any performance issues with one or the other in their game, depending on their target minimum device, ideally through quick profiling experimentation.

Just my 2c

1 Like

It’s a pretty good 2c.

Just my 2c. :slight_smile:

1 Like