Cocos2dx game design to separate animation and game logic

Cocos2dx game design to separate animation and game logic
0.0 0


Hello all,

I am a newbie in cocos2d framework. I would like to know is there any good design/structure to separate animation and game logic.

I have read some game sample developed by cocos2d, and they are all using animation callback to integrate the game logic (for example, when the eating animation is ended, call a function to add the health point).

When the game object grow huge, it’s difficult to trace this function is called by which animation, and it’s hard to debug and maintain the code.

Is there any better design?


Well, I know some people have tried to implement MVC:

I don’t think I would go into that much trouble unless I was making a really complex (or multiplayer) game tho’.


I tend to use a somewhat MVC type design.

I have Entity classes that represent each ‘thing’ in the game
Each Entity has one or more physical objects (essentially Box2d objects, but I try to be somewhat agnostic)
Each physical object has one or more Visible Objects - essentially classes that hold sprite and other graphic information.

I have a GameController that runs the show.

I make everything as generic as I can, with the code in virtual methods in a base class - so specific functionality can be implemented where necessary.

So in your example of an eating animation;

Each visible object has one or more animations. Each has a name - say Eating, Drinking, Dying, running, climbing - whatever is appropriate to that visible object.

When an event happens on the entity (say a collision between the player and an apple) the controller tells both entities that they have collided with the other entity.

The apple entity just destroys itself.

The Player entity looks at the class type (edible entity) and tells its visible objects (actually via its Physical objects) to perform the ‘eating’ function.

The physical objects just pass this request on to their visible objects

The visible objects say 'I need to eat - do I have an Eating animation? - Yes I do - start it up"

I don’t do a callback to tell the entity to add points, because the entity knows how long it takes to eat something (it’s all in the XML that defines the entities) so it counts down that time before adding the points (which are also defined in the XML)

So, if you like, it’s coincidence that the animation takes the same amount of time as it takes to add on the points - but one is just a graphical representation, while the other is an event happening in the ‘world’

Hope that helps - it actually helps me to get things straight in my head just to write them down!