Design Pattern for games in cocos2d-x c++

Hi.
What is your favourite design pattern for games in cocos2d-x c++ language?

I have some principles.
0. Use Node for base class.(Don’t use CCObject or your custom class for this!)
1- Don’t use singleton other than cocos singleton. (I mean Director)
2- Don’t use global variables. Add function parameters instead. (no problem with constants in global scope)
3- Don’t call public method of one class in another one, other than another one is parent of that class. (loose coupling)
4- Use custom events for communication between classes.
5- Use cocos action system for game logic. This system is very powerful.

What is your principles?

1 Like
  1. use Ref, if you need to.
  2. Singletons are fine :smile:
  3. So are globals, but I use very few.

These really aren’t design patterns, but rather an approach, I’d say.

http://gameprogrammingpatterns.com/

2 Likes

Thanks for information and link.

My personal approach are:-
If it makes sense use singleton whenever you can.
Avoid unnecessary inheritance, not more then 2-3 level.
Never used Ref though, but willing to.
Never use Static variables specially bool, if this is the end of world still avoid it.
use struct{} whenever you manage data and if game is not resource heavy.
Find out earlier what is better AOS vs SOA pattern with struct{} implementation for game performance.

Thanks

2 Likes

Theory vs practice. :smiley:

May be you will have interest to design patterns in cocos2d-x. This design patterns you use in your game.
design patterns in cocos2d-x

I use singleton for easy access of tool classes.
I use state machines for game logic.