Cocos2D-X-初始化CCTMXTiledMap时为EXC_BAD_ACCESS(代码= 2,地址= 0x0) [英] Cocos2D-X - EXC_BAD_ACCESS (code=2, address=0x0) when initializing a CCTMXTiledMap
问题描述
我正在尝试在Cocos2D-X中创建带有图块贴图的游戏,但出现以下错误:
I'm trying to create a game in Cocos2D-X with a tile map and I'm getting the below error:
EXC_BAD_ACCESS(代码= 2,地址= 0x0)在方法 CCTMXTiledMap :: initWithTMXFile(const char * tmxFile)的行 setContentSize(CCSizeZero); 中)
EXC_BAD_ACCESS (code=2, address=0x0) in the line setContentSize(CCSizeZero); of the method CCTMXTiledMap::initWithTMXFile(const char *tmxFile)
我以这种方式创建图块地图:
I'm creating the tile map in this way:
tileMap->initWithTMXFile("TileMap.tmx");
this->background = tileMap->layerNamed("Background");
有人知道发生了什么吗?
Somebody knows what it's happening?
请,你能帮我吗?
推荐答案
好的,我发现了问题.只是一点理论:
Ok, I've found the problem. Just a little of theory:
EXC_BAD_ACCESS code = 2 表示指针已损坏,可能由于以下原因而发生:
EXC_BAD_ACCESS code=2 means that the pointer is corrupt and this could happen because:
- 指针可能永远不会初始化.
- 由于您超出了数组的边界,所以指针可能被意外写入了.
- 指针可能是对象的一部分,该对象被错误地强制转换然后写入.
- 以上任何一种都可能破坏了另一个指针,该指针现在指向或靠近该指针,并且使用该指针会破坏该指针(依此类推).
因此,就我而言,我认为 initWithTMXFile
足以初始化 tileMap
指针,但我错了.解决方案是必须初始化 initWithTMXFile
, tileMap
之前的第一件事.因此,正确的代码块是:
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);
希望对您有帮助.
这篇关于Cocos2D-X-初始化CCTMXTiledMap时为EXC_BAD_ACCESS(代码= 2,地址= 0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!