5

Since currently it is not possible to set the color to of a SKScene to clearColor, by doing

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {


        self.backgroundColor = [SKColor clearColor];

    }
    return self;
}

As seen here: LINK

Then how can one set the background image for a SKScene? Please be as specific as possible, sample code would be great!

Community
  • 1
  • 1
vzm
  • 2,440
  • 6
  • 28
  • 47

4 Answers4

7

swift 4 version:

    let background = SKSpriteNode(imageNamed: "CheckIcon")
    background.size = frame.size
    background.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(background)
Paul Lehn
  • 3,202
  • 1
  • 24
  • 29
5

Use an SKSpriteNode centered in the scene:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        // Replace @"Spaceship" with your background image:
        SKSpriteNode *sn = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

        sn.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        sn.name = @"BACKGROUND";

        [self addChild:sn];
    }
    return self;
}
godel9
  • 7,340
  • 1
  • 33
  • 53
  • this is working partially, as its actually stretching my image, not sure why it would be doing this... – vzm Nov 05 '13 at 03:13
  • If your image is called Spaceship.png, it'll stretch the image by a factor of two for retina displays. You'll need a file called Spaceship@2x.png for retina displays. This file shouldn't be stretched. – godel9 Nov 05 '13 at 04:19
  • Also scene has a scaleMode property that defines how content of the scene gets scaled. – CodeSmile Nov 05 '13 at 10:19
  • Is there not a more handsome way of doing this? – Entalpi Aug 02 '14 at 23:22
0

This solution helped me :

Put your code which calls/adds the scene from viewDidLoad to viewWillLayoutSubviews

- (void)viewWillLayoutSubviews
    {
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.bounds.size.width*2,skView.bounds.size.height*2)];
    scene.scaleMode = SKSceneScaleModeAspectFill;

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

As far as I know the reason for this is that the skView.boundsare different when the view is about to be layed out and when the view is shown. In general I found that the height and width are switched.

I found this solution from here: Background image size Sprite Kit Game

Community
  • 1
  • 1
MB_iOSDeveloper
  • 4,178
  • 4
  • 24
  • 36
0

Here's a more elegant way, to ensure no matter what resolution you have now or in future, that your image will be the size of your background.

If you want to support banner ads, you'll have to reduce the height by 50 or 32 device units, and move the position up/down 25 or 16 pixels (portrait or landscape). I also recommend having a different image for landscape vs portrait if you want to support both, or you'll have to start letter-boxing.

In the scene...

SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground"];
background.size = self.frame.size;
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:background];
GilesDMiddleton
  • 2,279
  • 22
  • 31