1

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];
}
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
MrNaitX
  • 11
  • 2

2 Answers2

2

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.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
0

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.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157