第二次CCScene替换后应用程序崩溃

时间:2012-05-14 07:06:14

标签: iphone cocos2d-iphone

我正在使用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;
}

所以,我正在启动我的应用程序:

  1. 'createGame'名为
  2. 按'重启'按钮
  3. 'onExitPressedWithArgs'调用
  4. 'destroyGame'和'createGame'被称为
  5. 创造了新世界,一切顺利
  6. 按'重启'按钮,'onExitPressedWithArgs'调用,'destroyGame'和'createGame'被称为
  7. 应用程序崩溃。
  8. 它总是在cade的不同部分崩溃,但总是出现“EXC_BAD_ACCESS”异常。

2 个答案:

答案 0 :(得分:1)

[self.controllers release];方法移除destroyGame。正如您已经打电话给self.controllers = nil;,它将为您完成所需的工作。

答案 1 :(得分:0)

与Rishi的建议类似,但初始分配也存在问题。

1)替换它:

self.controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];

用这个:

controllers = [[NSArray alloc] initWithObjects:box2d, menu, controls, nil];

2)而且:

[self.controllers release];
self.controllers = nil;

使用:

[controllers release];
controllers = nil;