Oscar Apeland
Oscar Apeland

Reputation: 6662

"Tile map has no objects object layer" When object layer has objects

Im doing this tutorial

What I am doing is making a tile map in "Tiled", you cocos2d people probably know what i'm talking about. I have followed every step in the tutorial, but still, when I build and run, the app crashes immediately after the loading screen. It crashes with the error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'tile map has no objects object layer'

Or, with breakpoints, it breaks at:

NSAssert(objectGroup != nil, @"tile map has no objects object layer");

I compared my own .tmx file to the one in the tutorial download, and they match up. Relevant .tmx code:

<objectgroup name="Objects" width="15" height="13">
<object name="SpawnPoint" x="35" y="36"/>
</objectgroup>

You would think this counts as objects in the object layer, right? I'm sure the SpawnPoint object is in the correct layer in Tiled aswell. Here is my init method (the method the app crashes in):

-(id) init
{
if( (self=[super init]) ) {
    CCTMXObjectGroup *objectGroup = [_tileMap objectGroupNamed:@"Objects"];
    NSAssert(objectGroup != nil, @"tile map has no objects object layer");

    NSDictionary *spawnPoint = [objectGroup objectNamed:@"SpawnPoint"];
    int x = [spawnPoint[@"x"] integerValue];
    int y = [spawnPoint[@"y"] integerValue];

    _player = [CCSprite spriteWithFile:@"player.png"];
    _player.position = ccp(x,y);

    [self addChild:_player];

    self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"TileBomb.tmx"];
    self.background = [_tileMap layerNamed:@"Background"];
    self.meta = [_tileMap layerNamed:@"Meta"];
    _meta.visible = NO;
    [self addChild:_tileMap z:-1];
    self.touchEnabled = YES;


}
return self;
}

Does anyone understand why this is happening & how to fix it?

Upvotes: 1

Views: 691

Answers (1)

Oscar Apeland
Oscar Apeland

Reputation: 6662

Solved it. Always get the document before using the document. The init method read objects from the tilemap before importing it. Leaving this here for reference for people equally nooby as me

Upvotes: 1

Related Questions