Question

Most of the tutorials that I've read only explain Cocos2D examples in the HelloWorld class, but as I've started to build a simple game I need to know how to send an event to different classes, and for them to respond whenever it happens.

I have GameSceneLayer, a CCLayer class which loads in my different CCLayers of Sprites:

@implementation GameSceneLayer
+ (CCScene *)scene {
    CCScene *scene = [CCScene node]; // Create a container scene instance
    GameSceneLayer *gameLayer = [GameSceneLayer node]; // Create an instance of the current layer class
    [scene addChild:gameLayer]; // Add new layer to container scene
    return scene; // Return ready-made scene and layer in one
}
-(id)init
{
self = [super init];
if (self != nil)
{
    Background *background = [Background node];
    [self addChild:background z:0];
    Player *player = [player node];
    [self addChild:player z:1];
    MainMenu *mainMenu = [MainMenu node];
    [self addChild:mainMenu z:2];

}
return self;
}
@end

However, when my MainMenu CCLayer START sprite is touched I would like it to spawn in the PLAYER sprite from the Player CCLayer.

I'm guessing that I need a GlobalVariables.h with something like:

 #define gameStart @"0"

So when the START sprite is pressed it changes gameStart to 1, and somewhere in the PLAYER sprite there is

 if (gameStart == 1)
{
    [self addChild:PLAYER];
}

However I'm not sure how to set up the code so that the PLAYER sprite is always looking for that information.

Was it helpful?

Solution

You have objects (instances of classes). You want one object to communicate with another. In Objective-C, that's called sending messages. In other languages it's simply calling a method.

You don't need global variables. Instead, the receiving object MainMenu needs to send a message to (call a method on) the Player object. How do you get two objects to know each other? You could put them in a stinky, loud, overcrowded discotheque and hope for the best.

Or you could simply let them talk to each other, but alas, they shouldn't. Since both are siblings of GameSceneLayer, they shouldn't hold references to each other themselves (danger of creating retain cycles unless you're using weak references).

But both have the same parent. So what does a good parent do when two siblings won't talk to each other? It relays the message!

In MainMenu, send a message to the parent GameSceneLayer:

[(GameSceneLayer*)self.parent gameWillStart];

The GameSceneLayer implements that selector, and forwards the message to any other object that should be informed about starting the game:

-(void) gameWillStart
{
    [player gameWillStart];
    [ingameUI gameWillStart];
    // etc.
}

And Player also implements said selector:

-(void) gameWillStart
{
    [self addChild:PLAYER];
}

Just one other thing: in GameSceneLayer, make player and all other objects ivars (instance variables) so that GameSceneLayer has these references readily available. The alternative would be to tag (less often used) objects and then use getChildByTag:

PS: adding PLAYER as child looks dubious to me. If you have already created whatever node PLAYER is, you should add it right away and if necessary, set it to be invisible and/or paused while the game hasn't started yet.

OTHER TIPS

You can use a singleton GameState Manager, which keeps information on the game state.

Here is a little snippet:

+(GameManager*) sharedGameManager {
if (!_sharedGameManager) {
    _sharedGameManager = [[self alloc] init];
}
return _sharedGameManager;
}

+(id) alloc {
    NSAssert(_sharedGameManager == nil, @"Cannot create second instance of singleton Game Manager");
    return [super alloc];
}

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

in that game manager you can have an enum with the game states, and set a property for it.

//Header

typedef enum {
     kGameStarted,
     kGameEnded
 }GameState

@interface GameManager : CCNode {

}
@property (nonatomic) GameSate gameState;

Then back in your implementation file, you synthesize that GameState property, and create your own setter

//Implementation
@synthesize gameState = _gameState;

//Create your own setter, so you can notify all listeners
    -(void) setGameState:(GameState) newGameState {
    if (newGameState != _gameState) {
        _gameState = newGameState;

        //Notify listeners via NSNotification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"gameState" object:nil];
    }
}

in the classes where you want to get the messages you just have to subscribe to the "gameState" notification like so:

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(onGameStateChange:) 
        name:@"gameState"
        object:nil];



  -(void) onGameStateChange{
          if ([GameManager sharedManager].gameState == kGameStarted){
          //start game
          }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top