MrNaitX
MrNaitX

Reputation: 11

Sprite Kit, Grey background instead of image

I can't seem to get my background to show the image.

Here is the code:

@implementation GameScene

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

        self.anchorPoint = CGPointMake(0.5, 0.5);

        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
        [self addChild:background];
    }
    return self;
}

@end

 - (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 unarchiveFromFile:@"GameScene"];
    scene.scaleMode = SKSceneScaleModeResizeFill;

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

Upvotes: 1

Views: 468

Answers (2)

Whirlwind
Whirlwind

Reputation: 13675

As an addition to @SkylerLauren answer instead of initWithSize:, you should use initWithCoder: method. More on this link.

And if you explicitly want to use scene's init method you should be aware that self.view is nil while initialization.

Upvotes: 0

Skyler Lauren
Skyler Lauren

Reputation: 3812

I think the issue is initWithSize: isn't actually called when you unarchiveFromFile: instead try using didMoveToView: in your scene.

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    [self addChild:background];
}

Hopefully that helps.

Upvotes: 2

Related Questions