Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 207
vzm Avatar asked Nov 05 '13 01:11

vzm


2 Answers

swift 4 version:

    let background = SKSpriteNode(imageNamed: "CheckIcon")
    background.size = frame.size
    background.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(background)
like image 57
Paul Lehn Avatar answered Nov 10 '22 00:11

Paul Lehn


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;
}
like image 35
godel9 Avatar answered Nov 10 '22 00:11

godel9