1

When I am setting a background image on a SKScene. I have written the code below:

#import "JTEDMyScene.h"
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
@interface MyScene()
@property BOOL isContentCreated;
@end

@implementation MyScene

-(void) didMoveToView:(SKView *)view
{

 if(!self.isContentCreated)
 {
    self.isContentCreated = YES;
    [self createSceneContent];
 }
}

-(void)createSceneContent
{
 [self addChild:[self createBackground]];
}

-(SKSpriteNode *)createBackground
{
SKSpriteNode *backgroundSprite;
SKTexture *textureImage;
if (IS_IPAD) {
    textureImage = [SKTexture textureWithImageNamed:@"home_ipad1.jpg"];
    backgroundSprite = [SKSpriteNode spriteNodeWithTexture:textureImage];
}
backgroundSprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

return backgroundSprite;
}

Device orientation modes are only Landscape left and landscape right. My image size is width = 1024 and height= 768 and 264 dpi. But the image is not showing fully a portion of image is showing.

Brian Nickel
  • 26,890
  • 5
  • 80
  • 110

4 Answers4

1

Try to present and create MyScene in viewWillLayoutSubviews - if you haven't done it already. It may work if you just rename viewDidLoad to viewWillLayoutSubviews and rename the super call too.

Eran
  • 387,369
  • 54
  • 702
  • 768
Maq
  • 369
  • 3
  • 13
1

Maq has the right idea. I think what he's saying is, the problem lies in your view controller .m file, instead of in your Scene. Instead of creating your SKView and SKScene and presenting the SKScene from within the viewDidLoad method of your VC, put that code in the viewWillLayoutSubviews method instead. Something along the lines of this:

-(void)viewWillLayoutSubviews  {
  [super viewWillLayoutSubviews];

  SKView * skView = (SKView *)self.view;
    SKScene * scene = [YourScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}

Apparently, viewWillLayoutSubviews gets called later than viewDidLoad. When viewDidLoad is called, the view hasn't even been added to the view hierarchy yet.

Ray Wenderlich's Sprite Kit Tutorial for Beginners quotes this answer from SO as to why this happens.

Community
  • 1
  • 1
baptzmoffire
  • 589
  • 2
  • 4
  • 20
0

I started a blank project and copy-pasted your code into it and it works fine. I know you said you set your device orientation, but did you do that in your project settings? That's the only problem I can think of. Select your project, general tab, and under Deployment Info, there's a "Devices" drop-down box. Select iPad and make sure "Landscape Left" and "Landscape Right" are the only two checked. My guess is that the project thinks it's supposed to be starting in portrait mode, so it's stretching your 768-tall image vertically to 1024 at start; then when your app determines that it's in landscape orientation, it just rotates that stretched image accordingly, which would result in the image being cut off.

baptzmoffire
  • 589
  • 2
  • 4
  • 20
  • Yah I have checked only "Landscape Left" and "Landscape Right". Here my screen width is 1024 and height is 768. –  Oct 03 '13 at 15:21
0

yes, to control and resize background image (in my case) correspond to device orientation, you have to use -(void)viewWillLayoutSubviews

but don't forget, it will start from first scene by default, so you need to use some "sceneNumber" selector in this function to point to actual scene

djdance
  • 3,110
  • 27
  • 33