spaderdabomb
spaderdabomb

Reputation: 942

Objective-C can't tell where the instances of pointers are coming from

I'm a newb for Objective-C...hopefully the terminology in the title is correct...but anyhow. I am following a tutorial and was a bit confused where instances of certain pointers were showing up. I never saw them explicitly defined, and then later the tutorial would modify them. For an example, look below (I tried to include only the necessary code. I put stars * next to the lines that I am confused by the most. Basically I'm not understanding where _meta and _hud are coming from and how I can call methods from them. I guess I would be less confused if they were hud and meta, without the _). Thanks, sorry if it's an amateur question.

@interface PlayGameLayer()

@property (strong) CCTMXLayer *meta;
@property (strong) HudLayer *hud;

@end

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    PlayGameLayer *layer = [PlayGameLayer node];
    [scene addChild: layer];
    HudLayer *hud = [HudLayer node];
    [scene addChild:hud];
    layer.hud = hud;

    return scene;
}

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

    ....

    self.meta = [_tileMap layerNamed:@"Meta"];
    _meta.visible = NO; **********************************************

    ....


    }
    return self;
}

-(void)setPlayerPosition:(CGPoint)position {
    CGPoint tileCoord = [self tileCoordForPosition:position];
    int tileGid = [_meta tileGIDAt:tileCoord];
    if (tileGid){
        NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
        if (properties){
            NSString *collectible = properties[@"Collectable"];
            if (collectible && [collectible isEqualToString:@"True"])
            {
                [_meta removeTileAt:tileCoord]; ****************************************
                self.numCollected++;
                [_hud numCollectedChanged:_numCollected];*********************************
            }
        }
    }

    _player.position = position;
}


@implementation HudLayer
{
    CCLabelTTF *_label;
}

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

       ....

    }
    return self;
}

@end

Upvotes: 0

Views: 55

Answers (1)

Chris Vasselli
Chris Vasselli

Reputation: 13344

The instance variables _meta and _hud are implicitly generated by the compiler, as a result of your property definitions:

@property (strong) CCTMXLayer *meta;
@property (strong) HudLayer *hud;

This is a fairly recent addition to Objective-C. You used to need to use @synthesize in your .m file in order to create corresponding instance variables. However, starting from Xcode 4.4, if you don't include a @synthesize for a property, the compiler will automatically generate one for you. For your properties, it's implicitly generating:

@synthesize meta = _meta;
@synthesize hud = _hud;

Here's an article with more details if you're interested.

Upvotes: 3

Related Questions