0

I have a game menu, intro scene and game itself. If player is dead I would like to show game menu again. When game starts menu shows up. Pressing new game shows intro scene as it should. After some time of animation self.sceneEndCallback() is triggered and game scene is shown. When player is dead self.gameEndCallback() crashes with EXC_BAD_ACCESS.

GameViewController.m

        __weak GameViewController *weakSelf = self;
        IntroScene *introScene = [IntroScene nodeWithFileNamed:@"IntroScene"];
        introScene.scaleMode = SKSceneScaleModeAspectFill;
        [skView presentScene:introScene];
        introScene.sceneEndCallback = ^{
            GameScene *scene = [GameScene nodeWithFileNamed:@"GameScene"];
            scene.scaleMode = SKSceneScaleModeAspectFill;
            SKTransition *transition = [SKTransition fadeWithDuration:2];
            [skView presentScene:scene transition:transition];
            scene.gameEndCallback = ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            };

        };

inside GameScene.h

@property (nonatomic, copy) dispatch_block_t gameEndCallback;
HelloimDarius
  • 695
  • 5
  • 23
  • 1
    1. Please, provide crash log. 2. Are you checking that `gameEndCallback` is not `NULL` before invoking it? – Borys Verebskyi Mar 04 '16 at 21:16
  • Not directly related to your crash, but you should also re-grab a strong reference to self and check for nil before referencing it rather than directly referencing weakSelf like you're doing here. See http://stackoverflow.com/questions/13257356/what-is-the-proper-way-to-avoid-retain-cycle-while-using-blocks/13257892#13257892 – fullofsquirrels Mar 04 '16 at 21:29
  • @BorisVerebsky you're right. gameEndCallback is NULL. But why? – HelloimDarius Mar 04 '16 at 21:56

1 Answers1

0

Is anything actually calling the introScene.sceneEndCallback? Because, without that, scene.gameEndCallback will be NULL and attempting to call it will crash with BAD_ACCESS.

bbum
  • 162,346
  • 23
  • 271
  • 359