0

Ok, i've been trying to figure this out and searching the web and the forum but I haven't had any luck yet.

What I want to do is the following. I have a MainMenuScene which is a SKScene. Since I have added a number of SKActions and other things in that scene I wanted to be able to break it up into different files. So, for example, when the player presses the "Settings" button I would use a different file to write what would happen instead of using the MainMenuScene and make it even larger.

So my question is this: I want to be able to reference the SKScene in MainMenuScene.m from my SettingsSubScene.m file. The SettingsSubScene.m is the following:

#import "SettingsSubScene.h"

@implementation SettingsSubScene
{
 SKScene *_mainMenuScene;
}

-(id)initWithScene:(SKScene *)mainMenuScene {

 _mainMenuScene = mainMenuScene;

 return self;
}

-(void)displaySettings {
SKSpriteNode *settingsGearLarge = (SKSpriteNode *)[_mainMenuScene childNodeWithName:@"settingsGearLarge"];

SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"settings"];

background.anchorPoint = CGPointMake(0, 1);
background.position = settingsGearLarge.position;
background.zPosition = 0;
background.xScale = 0;
background.yScale = 0;

[_mainMenuScene addChild:background];

SKAction *appear = [SKAction scaleTo:1.0 duration:0.5];
appear.timingMode = SKActionTimingEaseInEaseOut;

[background runAction:appear completion:^{
    [SKActionEffects fullScale:background amount:0.05 forever:YES];
    [SKActionEffects fullWiggle:background amount:1 forever:YES];
}];

}

-(void)hideSettings {

}

@end

And I call it from MainMenuScene.m using the following code:

SettingsSubScene *settingsSubScene = [[SettingsSubScene alloc] initWithScene:self];
        [settingsSubScene displaySettings];

And I get an exc_bad_access error. Of course i'm doing something wrong and I bet there is a way to do this instead of creating a large MainMenuScene file but I haven't found it yet. Can anyone help please?

Thanks

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
μ4ρκ05
  • 585
  • 2
  • 5
  • 16
  • What line is the error on? Add an exception breakpoint to find out. Do you get any compiler warning? Analyze and fix them, they are trying to tell you something. Also if your extra classes are inheriting from SK*Node classes you also already have access to the scene via the self.scene property. – CodeSmile Jan 21 '14 at 20:59

3 Answers3

0

Modify your initWithScene: method like so:

-(id)initWithScene:(SKScene *)mainMenuScene {

    if (self = [super init])
    {
        _mainMenuScene = mainMenuScene;
    }

 return self;
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
0

A friend found the answer.

I needed to call:

@implementation SettingsSubScene
{
     MainMenuScene *_mainMenuScene;
}

instead of just

@implementation SettingsSubScene
{
     SKScene *_mainMenuScene;
}

since my mainMenuScene is not just a SKScene but extends it.

μ4ρκ05
  • 585
  • 2
  • 5
  • 16
  • Probably I didn't make myself clear with what I was looking for. The above worked. Even more, I didn't actually need to have an object class so I could create static methods as for example "+(void)displaySettingsInMainMenuScene:(MainMenuScene *)mainMenuScene {". I just used the SKScene instead of the extended MainMenuScene before and that's why it didn't work. I want to thank everyone for their responses! – μ4ρκ05 Jan 22 '14 at 13:44
0

As akashg has said above, you are missing the init in your initWithScene override.

joshd
  • 1,626
  • 14
  • 17