Ok, I've found the problem. Just a little of theory:
EXC_BAD_ACCESS code=2 means that the pointer is corrupt and this could happen because:
- The pointer could have never been initialized.
- The pointer could have been accidentally written over because you overstepped the bounds of an array.
- The pointer could be part of an object that was casted incorrectly, and then written to.
- Any of the above could have corrupted a different pointer that now points at or near this pointer, and using that one corrupts this one (and so on).
So, in my case, I thought that initWithTMXFile
was good enough to initialize the tileMap
pointer but I was wrong. The solution is that first thing before initWithTMXFile
, tileMap
must be initialized. So, the correct block of code is:
this->tileMap = new CCTMXTiledMap();
this->tileMap->initWithTMXFile("TileMap.tmx");
this->background = tileMap->layerNamed("Background");
this->addChild(tileMap);
I hope it helps.