Using CCNotificationCenter to create achievements in a game

Using CCNotificationCenter to create achievements in a game
0.0 0


I try to create achievements in my game, it’s easy to create it using CCNotificationCenter?
I take a look in the test folder, but I don’t fully understand the concept of CCNotificationCenter.


to add a listener, define a event name, and put it somewhere where everything can access it (like in a header file that is imported by everything that needs it)
like this:

#define EVENT_GOT_BONUS “Evnt_gt_bns”

then to make an object listen to this event, do something like this in init of MyClass

CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(MyClass::listenGotBonus), EVENT_GOT_BONUS, NULL);
“this” means the MyClass that is calling this line (eg in its init function)
MyClass::listenGotBonus is a function inside Myclass that must take a single CCObject input

this function will fire ANY time that this line is run from anywhere:

The object input of the chosen function will be whatever you put in “this” for this function… eg if you want to know exactly what object called the post.

to remove the listener run:
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVENT_GOT_BONUS);
in the Myclass distructor (yeah, im pretty sure it can be called here because the notification center doesn’t retain it)

if an object wants to track only events from a particular object you use the last argument:

CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(FlickTarget::listenBallMove), EVENT_ITEM_MOVE, nFollowBowl);
and still remove with:
this will only fire the selector when it is the object that posts the event.
one thing that isnt obvious:
Each listening object can only track each event once… eg, you cant track EVENT_ITEM_MOVE for more than one “ball”~~in my case from the SAME listener….
you can track EVENT_ITEM_MOVE for multiple objects if it is different objects listening… they will all ignore any other EVENT_ITEM_MOVE except for the object they are listening to.

Does that help?


It help, thanks a lot
I’ll try to use it, if I find problems, I’ll post more about it.
Thanks daniel fountain.