Change background colours of a SKscene after it has been presented using a timer

So I have setup a skscene within a Skview on an app I'm testing. I setup the background color etc like so.

    SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
    SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];

    // Set the scale mode to scale to fit the window
    scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
    scene.scaleMode = SKSceneScaleModeAspectFit;
    [skBG presentScene:scene];

    NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
    SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
    snowParticle.particlePosition = CGPointMake(160, 284);

    scene addChild:snowParticle];
    [self.view addSubview:skBG];

Then I want to change the background background color randomly on the scene thats already presented. I've attached a standard timer being called after 5 seconds and in it does something like this:

self.scene.backgroundColor = [SKColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];

But it doesn't update? I tried representing the scene with '[skView presentScene:scene];' but doesn't work.

Answers


Hm, not all that much to go on, but from what you've actually shared it might be that you're not actually displaying the self.scene property. In the code where you setup the SKView you have:

SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];

but this is not the same as the .scene property (unless you're doing a self.scene = scene; somewhere inside the method). From what little information we have my guess is that you're either not displaying the self.scene property or that you're covering it up with the new instant. If this is in deed the case then it should be as straightforward as changing what you got to:

SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
_scene = [SKScene sceneWithSize:self.view.bounds.size]; 

// Set the scale mode to scale to fit the window
self.scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
self.scene.scaleMode = SKSceneScaleModeAspectFit;
[skBG presentScene:self.scene];

NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
snowParticle.particlePosition = CGPointMake(160, 284);

self.scene addChild:snowParticle];
[self.view addSubview:skBG];

Need Your Help