Search code examples
iosuiviewios7spritesprite-kit

How to set the background image for a SKScene?


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!


Solution

  • 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;
    }