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.
Simplest way to avoid this problem is to use forward declarations.
In .h file of GameObject1 just declare
and in GameObject1.m include GameLevel1Layer.h
In .h file of GameLevel1Layer just declare
and in GameLevel1Layer.m include GameObject1.h