0

I'm using the root viewcontroller to display a couple of labels to keep track of scores etc, which is working fine. However, the root viewcontroller is not responding to messages from the scene right away when the scene is loaded.

The situation is this: UIViewController presents SKView where I initialize MyScene on top of that (normal SpriteKit stuff). Immediately in MyScene I load the level from a json-file, which contains the number of possible points on that specific level. When the level is done loaded, I try to send the number of possible points back to my root viewcontroller, so it can display it in a label.

I've tried setting a property myScene.mainViewController = self from the viewWillLayoutSubviews on ViewController, and then just [self.mainViewController setInitialScore:_bgLayer.maxScore];, but the method setInitialScore never fires. I've also tried using the NSNotificationCenter, but this does not work either.

However, I call a method in self.mainViewController whenever the player collects a point (which happens a little bit later), and that works fine. So the problems seems to be that the ViewController is not done loading or something, so it doesn't respond right away.

How could I solve this?

EDIT 1: Here's some code by request:

ViewController.m

- (void)viewWillLayoutSubviews {
  [super viewWillLayoutSubviews];

  SKView *skView = (SKView *)self.view;
  if (!skView.scene) {
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    MyScene* scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    scene.mainViewController = self;

    // Present the scene.
    [skView presentScene:scene];

    self.numLives = bStartNumLives;
    self.score = 0;
    [self resetLabels];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setInitialRings:) name:@"setInitialRings" object:nil];
  }
}

- (void)setInitialRings:(NSNotification *)notification {
  NSLog(@"Initial rings set");
  NSDictionary *userInfo = [notification userInfo];
  NSInteger rings = [(NSNumber *)[userInfo objectForKey:@"rings"] integerValue];

  self.numRings = rings;

  [self resetLabels];
}

MyScene.m

- (void)createWorld {
  _bgLayer = [self createScenery];
  NSLog(@"world created setting initial rings");
  NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:_bgLayer.numRings] forKey:@"rings"];
  [[NSNotificationCenter defaultCenter] postNotificationName:@"setInitialRings" object:userInfo];

  _worldNode = [SKNode node];
  [_worldNode addChild:_bgLayer];
  [self addChild:_worldNode];

  self.anchorPoint = CGPointMake(0.5, 0.5);
  _worldNode.position = CGPointMake(-_bgLayer.layerSize.width / 2, -_bgLayer.layerSize.height / 2);
}

As you can see from this snippet of code, I'm trying to use the NSNotificationCenter. I've also tried calling [self.mainViewController setInitialRings:_bgLayer.numRings]; manually (but changing the setInitialRings-method to take an integer instead of a notification).

Aleksander
  • 2,735
  • 5
  • 34
  • 57
  • please provide the code with context rather than excerpts and explaining it - code is easier to read and less prone to misunderstandings. My guess is that self.mainViewController is assigned after you first use it – CodeSmile Jul 19 '14 at 21:15
  • LearnCocos2D - I just added some code. Hope that's good :) – Aleksander Jul 19 '14 at 21:23
  • 1
    keep in mind when you create the MyScene instance via sceneWithSize that's when MyScene init runs - meaning it runs before you add the notification observer and if createWorld is called from MyScene init the setInitial.. notification is posted with no active observer – CodeSmile Jul 19 '14 at 23:54
  • Why not simply use a delegation pattern for this? – Theis Egeberg Jul 20 '14 at 08:55

0 Answers0