12

I have an issue where my SKView's background color (grey) is briefly being displayed before the scene is presented.

I manually attempted to set it, both via the Storyboard editor and in my controller (self.skView.backgroundColor = [SKColor blueColor]) but it remains grey. Is this not an attribute that can be overridden?

Update #1

Here's a little explanation as to what happening:

  1. viewDidLoad is called and skView is presented on the screen w/ a grey background (skView.scene is not set yet).
  2. I load all of the game's assets (which takes ~1 second) and during this point the grey background is visible.
  3. After the assets have been loaded I load the scene and present it (the grey screen is replaced w/ the contents of the scene)

ViewController

- (void)viewDidLoad
{
    [self.activityIndicator startAnimating];
    [self authenticatePlayer];

    self.skView.backgroundColor = [SKColor blueColor];

    // During this time the grey screen is visible as the assets are being loaded and take ~1 second
    // self.skView.scene is NULL here so I cannot set the backgroundColor...

    [GamePlayScene loadSceneAssetsWithCompletionHandler:^{
        [self.activityIndicator stopAnimating];
        self.activityIndicator.hidden = YES;

        CGSize viewSize = self.skView.bounds.size;

        self.gamePlayScene = [[GamePlayScene alloc] initWithSize:viewSize];
        self.adView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [self.skView presentScene:self.gamePlayScene];

    }];
    ...

4 Answers 4

3

I'm afraid you'll either have to present your scene with the most basic setup done (like setting backgroundColor) and then load the rest, or present a "loading" scene until the gameplay scene loads, then replace it.

2
  • The problem is that self.skView.scene is null for a brief period (see my updated question).
    – Kyle Decot
    Feb 26, 2014 at 21:55
  • 1
    Yeah, seems like SKView is backed by CAEAGLLayer just like GLKView is. So that's where the gray color comes from. Changing that is possible (there are some answers on SO), but I think for this case it's a bit of an overkill. Take a look at my updated answer. Feb 26, 2014 at 22:20
0

I think you have mistaken it for the SKScene property of the same name:

self.scene.backgroundColor = [SKColor blueColor];

Note that self.scene (also self.view) will be nil until after the node has been added as child to another node (that is already part of the scene graph) respectively until the scene was presented.

7
  • 1
    Doesn't SKView just inherit from UIView (which has a backgroundColor property)?
    – Kyle Decot
    Feb 26, 2014 at 21:47
  • Also, I've updated my question w/ some more details.
    – Kyle Decot
    Feb 26, 2014 at 21:50
  • More details not needed, the case is pretty clear. You need to change the SKScene's backgroundColor property, not the view's. The view has the same property but the scene is what is being rendered (opaque) into/onto the view, so only the scene's background color takes effect.
    – CodeSmile
    Feb 26, 2014 at 23:38
  • 1
    skView.scene is null (at the point I'm talking about) so there is no way to set it.
    – Kyle Decot
    Feb 27, 2014 at 3:55
  • 1
    ^ That's what I ended up doing and that seems to be an acceptable solution (so far).
    – Kyle Decot
    Feb 27, 2014 at 18:02
0

You should be able to set the SkView background colour in the presenting view controllers viewDidLoad - prior to presenting the view. Is this not happening?

Providing you've added it to a parent node

0

Using the basic template for sprite kit I was able to set the background to black with this code inside of the GameViewController.m file:

- (void)viewDidLoad
{
[super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;

// Create and configure the scene.
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.backgroundColor = [UIColor blackColor];

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

Not sure if this is exactly what you were looking for but it got rid of the grey background that didn't seem to go away. In your case I think you would want to set the "gamePlayScene" background color property after initializing the scene.

Hope this helps!

2
  • OP asked for SKView background color, not SKScene Dec 15, 2015 at 14:36
  • 1
    I understand it wasn't exactly what the OP wanted but it works and might be the easiest solution. I don't think it has any adverse effects either so doing one vs the other is irrelevant. :)
    – johnnelm9r
    Dec 15, 2015 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.