1

my question is about communicating two directions from one class to its child.

I have a GameLayer CCLayer with a child GameObject CCNode. The GameLayer is a semi-singleton shared layer. I need to import in GameLayer's header the GameObject.h to be able to init the GameObject. I am now trying to communicate back to the gamelayer and I'm stuck. THe code all works until the questions.

static GameLevel1Layer* sharedGameLayer;

+(GameLevel1Layer*) sharedGameLayer
{
    NSAssert(sharedGameLayer != nil, @"shared game layer not there yet");
    return sharedGameLayer;
}

On init, I init the GameObject

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

    GameObject1* gameObject1 = [GameObject1 gameObject1];
    fish1.position = CGPointMake(0, 0);
    fish1.tag = kFish1TagValue;
    [self addChild:gameObject1 z:10];
    }
return self;
}

in game object (which is a node but basically inits a ccsprite)

+(id) gameObject1{
    return [[self alloc] initWithFish1Image];
    }

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

        CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
        [frameCache addSpriteFramesWithFile:@"fish1atlas.plist"];

        sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"fish1atlas.png"]; 
        [self addChild:sceneSpriteBatchNode z:0]; 

        fish1Sprite = [[CCSprite alloc] initWithSpriteFrameName:@"fish1_normal_1.png"];
        fish1Sprite.tag = kFish1SpriteTag;
        fish1Sprite.rotation = -30;
        [self addChild:fish1Sprite];


        }
    return self;
    }

My problem is that from within GameObject I'm trying to send a message to GameLayer1. If I include GameLayer1.h the argument gets circular and I get an undeclared identifier confusion. If I just try: [GameLayer1 sharedGameLayer] methodImTrying]; It doesnt recgonize and I get an undeclared identifier.

I tried adding: @class GameLayer1; and when I send a message to GameLayer it fails that "class message is a forward declaration".

[self.parent method]; and [super method] both fail.

I thought using a shared layer would allow me to access the parent node without having to import the header for the gamelayer in the gameobject. I know this is a basic question of objective-c, any help would be appreciated.

UPDATE:

If I instead import GameLevel1Layer.h into GameObject's header and add the @class GameObject to GameLayer, I can call [GameLevel1Layer sharedGameLayer] method]; i wonder if i'm doing this all quite wrong.

kidnim
  • 1,477
  • 2
  • 10
  • 13
  • 2
    What's a "semi-singleton"? Either you can instantiate a class more than once or you can't. – Caleb Oct 26 '12 at 02:40
  • its the term used by Steffen Itterheim in his book on cocos2d. I dont know why he says semi, I just repeat the term. I want a singleton and I think the idea is to be able to access the layer from the multiple children. In re-reading the section I think he uses some properties in the equivalent of the GameLayer to set the children and recall them. I'll look into that and follow up. – kidnim Oct 26 '12 at 03:21
  • I'm thinking now my problem is a class vs instance issue. I said in reply to Bijoy, I have another singleton, GlobalData, that works as expected and whose properties can be accessed by any class for instance. I can send messages there no problem: [[GlobalData sharedGlobalData] method]; – kidnim Oct 26 '12 at 13:06

3 Answers3

1

Simplest way to avoid this problem is to use forward declarations.

In .h file of GameObject1 just declare

 @class GameLevel1Layer 

and in GameObject1.m include GameLevel1Layer.h

In .h file of GameLevel1Layer just declare

 @class  GameObject1

and in GameLevel1Layer.m include GameObject1.h

Guru
  • 21,652
  • 10
  • 63
  • 102
  • Thanks, this worked. Unfortunately it doesnt work between GameLevel1Layer and my box2d Contact Listener. – kidnim Oct 27 '12 at 16:20
  • its cpp header, so you can add that in any one #import "MyContactListener.h" or u can add in prefix.pch also :) – Guru Oct 27 '12 at 17:06
  • ok. that worked. needed to do both: @class in header and import in implementation. phew – kidnim Oct 27 '12 at 17:20
0

You can use NSNotificationCenter to send and receive messages to and fro.

Check this post here that shows how you can send and receive messages:
Send and receive messages through NSNotificationCenter in Objective-C?

Also, check out the reference from Apple Developer Library: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Community
  • 1
  • 1
Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70
  • Thanks for the reply Bijoy. I was under the impression I could use singletons as an alternative method to communicate between classes to NSNotificationCenter. I have another singleton, GlobalData, that works as expected and whose properties can be accessed by any class for instance. I can send messages there no problem. – kidnim Oct 26 '12 at 13:03
0

Communicate back could be implemented with delegates. Create a @protocol and support it by your GameLayer. When create a GameObject set GameLayer as a delegate. When you need to send something to GameLayer just call

[delegate someMethod:withObject];
anatoliy_v
  • 1,782
  • 15
  • 24