Background image size Sprite Kit Game Background image size Sprite Kit Game ios ios

Background image size Sprite Kit Game

The short answer to your immediate problem:

background.size = self.frame.size;

The long answer, and discussion about other methods...

The scene works in Logical Units, not physical pixels (as mentioned in other posts).

1 Logical Unit is typically 1 pixel on older kit, and 2 pixels on newer kit/iPads.This maybe 4 pixels in 5 years time.

Working in Logical Units protects you from sizes changing in future, and is supposed to help the programmer - although this often confuses newbies.

The simplest way to get an image to fill the current scene is to set its size in 'logical units' no matter what size it is originally.

This is better than using SKActions (because the user might end up seeing them, and any calculations based on the dimensions of the node can't be relied upon until the action has completed), and it's better than scaling because scaling requires you to know the original image dimensions.

You can do this via the size property, or through an action if you really want to make an animation.

In the scene simply....

-(void)didMoveToView:(SKView *)view{    [super didMoveToView:view];    SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground"];    background.size = self.frame.size;    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));    [self addChild:background];} 

Alternatively, if your image works well, as is, on higher resolutions, you can rename your image to myBackground@2x.png, add that to your project and you probably won't need to resize at all, but you'll also need to shrink it by 50% for the lower resolution devices, and save that as myBackground.png.

I always set the size of an imported image to exactly the logical units I want (or a percentage of the screen dimensions) to preserve my sanity.

I've found that if an image is not the same size as the node you are looking to create the sizing gets a little funny (say using a retina image on a non-retina device or incorrect image naming). You can scale the image to fill if you create the texture from the image and set the texture and the node size using something like the following:

SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"MyImage.png"];SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture size:self.view.frame.size];background.position = (CGPoint) {CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)};[scene addChild:background];

I'm not a 100% sure this if this will fix your issue or not but it might be worth a shot.

This line returns size in points, not pixels. This is made because different devices have different resolutions, but will have same sizes in points. On non retina devices 1 point is 1 pixel and on retina devices 1 point is two pixels. Thats why you get this size from

SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];

You don't want to double the size of scene since it will just be our of bounds of your screen, invisible.