我正在构建一个 Cocos2D 游戏,在其中两个特定的 Sprite 发生碰撞后(通过简单的边界框技术),我调用
[[CCDirector sharedDirector] replaceScene:gameOverScene];
切换到游戏结束场景。
一旦它用其中的所有内容初始化游戏结束场景,游戏就会崩溃并转到 ccArray.m
中的这个方法。类:
void ccArrayRemoveAllObjects(ccArray *arr)
{ while( arr->num > 0 )
CC_ARC_RELEASE(arr->arr[--arr->num]);
}
消息: 线程 1:程序收到信号:“EXC_BAD_ACCESS”。
我尝试使用断点进行调试并发现一旦我的 gameOver 场景被初始化并准备好显示,dealloc
上一个类(称为替换场景的游戏类)中的方法被调用,然后抛出此错误。
我的 update
方法:
-(void)update:(ccTime)dt
{
if (CGRectIntersectsRect(plane.boundingBox, enemy.boundingBox)) {<br/>
CCScene *gameOverScene = [GameOverLayer sceneWithWon:NO];
[[CCDirector sharedDirector] replaceScene:gameOverScene];
} }
我的 dealloc
方法:
- (void) dealloc
{
[super dealloc];
[_monsters release];
_monsters = nil;
[mole release];
mole = nil;
[text release];
text = nil;
[mBG1 release];
mBG1 = nil;
[mBG2 release];
mBG2 = nil; }
我不知道为什么会这样。请帮忙。
谢谢你的期待。
最佳答案
在解除分配游戏场景之前,您正在调用 super 的 dealloc。 在你的 dealloc 方法结束时调用 super 的 dealloc :
- (void) dealloc {
[_monsters release];
_monsters = nil;
[mole release];
mole = nil;
[text release];
text = nil;
[mBG1 release];
mBG1 = nil;
[mBG2 release];
mBG2 = nil;
[super dealloc];
}
当您首先调用 super 的 dealloc
时,您将释放您的实例,使其成员无效,因此在 dealloc
之后访问它们将产生内存错误访问问题
关于ios - Dealloc 导致在 Cocos2D 场景之间切换时发生崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299083/