我正在使用 cocos2d 创建一个游戏。这是创建游戏时调用的方法:
- (void)createGame
{
GameScene *newScene = [[GameScene alloc] initWithController:self]; //subclass of CCScene
if ([CCDirector sharedDirector].runningScene)
[[CCDirector sharedDirector] replaceScene:newScene];
else
[[CCDirector sharedDirector] pushScene:newScene];
scene = newScene;
//some controllers for some layers of my scene
box2d = [[Box2DController alloc] initWithParent:self];
menu = [[MenuController alloc] initWithParent:self];
controls = ([[ControlsController alloc] initWithParent:self]);
self.controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];
//some object, contains some parameters. rizeEvent tells about some event to all controllers. In this case, it sends pointer to worldState to all of them.
worldState = [[WorldState alloc] init];
EventArgs *eventArgs1 = [[EventArgs alloc] initWithSender:self params:worldState];
[self riseEvent:@"WorldStateUpdate" withArgs:eventArgs1];
}
我有一个按钮,它破坏了我的世界,并创造了一个新的世界:
- (void)onExitPressedWithArgs:(EventArgs *)e
{
[self destroyGame];
[self createGame];
}
这是“destroyGame”方法:
- (void)destroyGame
{
[box2d release];
[menu release];
[controls release];
[scene release];
[worldState release];
box2d = nil;
menu = nil;
controls = nil;
scene = nil;
worldState = nil;
[self.controllers release];
self.controllers = nil;
}
所以,我正在启动我的应用程序:
- 'createGame' 调用
- 按下“重启”按钮
- 'onExitPressedWithArgs' 调用
- 'destroyGame' 和 'createGame' 被调用
- 新世界创造,一切顺利
- 按下“重启”按钮,调用“onExitPressedWithArgs”,调用“destroyGame”和“createGame”
- 应用程序崩溃。
它总是在 cade 的不同部分崩溃,但总是出现“EXC_BAD_ACCESS”异常。