define a cclayer in .h file

113 views Asked by At

In my I've defined a CCLayer like this:

@interface MyLayer : CCLayer {
    CCLayer * referenceLayer; 
}

How should I declare it to use it in +(CCScene *) scene ?

Like this ?

@property (nonatomic, retain) CCLayer *referenceLayer;
1

There are 1 answers

3
sergio On BEST ANSWER

Since + (id)scene is a class method, you cannot access an ivar/property from within it. One possible solution is having a static variable in your layer.m file, like in the following snippet:

static CCScene* _scene = nil;

+ (id)scene {
   if (_scene == nil) {
      _scene = [[CCScene node] retain];
      //-- further scene initializaion
   }
   return _scene;
 }

This simple approach has a drawback: you can only have one such layer around.