0

我正在使用 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. 应用程序崩溃。

它总是在 cade 的不同部分崩溃,但总是出现“EXC_BAD_ACCESS”异常。

4

2 回答 2

1

[self.controllers release];destroyGame方法中删除。正如您已经调用self.controllers = nil;的那样,它将为您完成所需的工作。

于 2012-05-14T07:30:05.177 回答
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;
于 2012-05-14T16:26:57.150 回答