2

I'm developing a rogue like game (iOS/Cocos2d). I have a object of class Map, which has bunch of GameObjects.

I have GameViewController class. When something in model happens (creature killed, player moved, anything), I need to notify GameViewController about this changes. How to do that?

I see two options:

  1. GameViewController instance could be a delegate of Map and a delegate of every GameObject.
  2. GameViewController will listen NSNotifications from Map and GameObjects.

Which way is better? Why?

1
  • Does GameViewController need to know about every GameObject? The Map contains the GameObjects, so it could watch them (being a delegate of, listening to notifications from, or giving a pointer to itself to the GameObjects) and in turn pass those messages to the GameViewController class (once again using any of those three techniques).
    – Cowirrie
    Apr 9, 2012 at 1:55

1 Answer 1

0

Cocos is a strongly singleton-based library, so the singleton method is quite common in Cocos games. That does not mean, however, it is the logical default choice.

Your second option is generally a wiser and safer approach for any program, not just Cocos. NSNotifications allow you to notify objects from other objects without getting into a potentially buggy state of passing numerous references or singletons around in your program. If you can achieve your goals with a few notifications, this is the way to go.

If you find you are using NSNotifications to an extreme degree, like multiple notifications every second, then you should consider either passing references around between your objects via pointers, or creating your parent class or main Cocos scene class as a singleton.

Just remember that when you get singletons involved, though they are easy to implement, it is just as easy to abuse them. You don't want to get into the habit of having children classes control the behavior of their siblings just because they can access the parent and the parent's methods easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.