Search code examples
objective-ccocos2d-iphonespritebuilder

Communicating which button is pressed in a pause menu using SpriteBuilder


I have a game with a pause menu button on it. When I press the button on my game scene, everything pauses normally, my pause menu shows up, then I can resume with one button and that also works.

I have another button for restarting the level. Since I'm loading in specific objects to the game, when I restart my level scene I need to remove all of these objects, otherwise I get an error for adding a child that has already been added. This means I need to somehow communicate to my game scene when I press restart to remove all of these objects.

Below is my code. Normally what I would do is create a variable within the pause scene that is accessible to my game scene. When the restart button is pressed, I would change the value of the variable within my pause scene, and using an update method in my game scene, I would be able to know when that variable's value is changed. Unfortunately, a couple problems arise when doing this with Spritebuilder. Normally you create a new scene by doing the following in Spritebuilder

CCScene *pauseScene = [CCBReader loadAsScene:@"PauseScene"];

If I do this, and I create a variable within my pause scene (as shown below) then I don't have access to it since I'm creating the pauseScene as an instance of CCScene, rather than my class PauseScene.

PauseScene.h

#import "CCScene.h"

@interface PauseScene : CCScene

@property (nonatomic, assign) BOOL restartTapped;

@end

Now if I try to instead create my pauseScene as an instance of my class PauseScene, then I get a warning Incompatible pointer types assigning 'PauseScene *' from 'CCScene *'. At runtime, if I ignore this warning, whenever I try to access restartTapped, it immediately errors and says unrecognized selector sent to instance.... So obviously that doesn't work.

I'm not really sure what to do. I have a tidy example of this problem below, just use the PauseScene.h from above as well as the ones below. If you have a solution please let me know! I guess I'm honestly just asking how to access variables from one class in another using Spritebuilder.

PauseScene.m

#import "PauseScene.h"

@implementation PauseScene

-(void)restartFromPause{
    [self removeFromParent];
    [[CCDirector sharedDirector] resume];
}

@end

MainScene.m

#import "MainScene.h"
#import "PauseScene.h"

@implementation MainScene

PauseScene *pauseScene;
float totalTime;

-(void)pauseFromMain {
    pauseScene = [CCBReader loadAsScene:@"PauseScene"];
    [[CCDirector sharedDirector] pause];
    [self addChild:pauseScene];
}

-(void)update:(CCTime)delta{
    totalTime += delta;
    CCLOG(@"total time %f", totalTime);
    CCLOG(@"value of other variable %i", pauseScene.restartTapped); //gives an error, details given in my post
}

@end

Solution

  • CCScene* pauseSceneCCScene = [CCBReader loadAsScene:@"PauseScene"];
    PauseScene *pauseScene = (PauseScene *)[pauseSceneCCScene children][0];
    

    Now you can access pauseScene variables just by doing pauseScene.variableName.