Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKScene iPad height width reversed

Tags:

ios

sprite-kit

I am trying to fill my SKScene with tiles, in an iPad app that only supports landscape mode. Within the scene I detect h & w as such:

int h = [UIScreen mainScreen].bounds.size.height;
int w = [UIScreen mainScreen].bounds.size.width;

Unfortunately, the dimensions sent back are the reverse of what they need to be. There are lots of topics in SO discussion this problem within the content of a view controller or a view, and the solution seems to be to detect screen size in viewWillAppear, not in viewDidLoad. This does not seem to be a valid solution for SKScenes, however.

Any suggestions?

like image 559
zeeple Avatar asked Oct 14 '13 05:10

zeeple


People also ask

What is the screen size of the latest iPad?

It is the most recent in 9.7 inch iPad version. The display is also Retina with a huge resolution of 2048 x 536 pixels at 264 ppi. This new iPad retained the size 9.7 inches display size (250 mm) and also the LED back-lighting technology, plus fingerprint and scratch resistant coating.

How do I Make my iPad screen look better?

If you’re designing for the web (using CSS or JavaScript) these values will be helpful. The iPad uses Retina screens which have a higher pixel density. This means they take the larger iPad resolution (mentioned below) and compress those pixels into a smaller space to make the image look sharper.

What is iPad screen size multiplier?

This means they take the larger iPad resolution (mentioned below) and compress those pixels into a smaller space to make the image look sharper. This is the full number of pixels that are being rendered. This is the value you get when you apply the multiplier (1x, 2x, 3x) the device uses to the screen size in points.

What is retina resolution on iPad?

The iPad uses Retina screens which have a higher pixel density. This means they take the larger iPad resolution (mentioned below) and compress those pixels into a smaller space to make the image look sharper. This is the full number of pixels that are being rendered.


Video Answer


2 Answers

Try, not using viewDidLoad and use this

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

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

        // Present the scene.
        [skView presentScene:scene];
    }
}
like image 55
DogCoffee Avatar answered Nov 15 '22 07:11

DogCoffee


It doesn't make sense to initialize the scene in the layout handler. Resize the scene in the layout handler. (That's what it's for.)

@implementation SGOMyScene
{
    SKScene *scene;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    SKView *skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    scene = [SGOMyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    [skView presentScene:scene];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView *skView = (SKView *)self.view;
    scene.size = skView.bounds.size;
}

Your scene should implement - (void)didChangeSize:(CGSize)oldSize to move everything into the right place.

Hooray, now your game handles device rotations too.

like image 41
alltom Avatar answered Nov 15 '22 06:11

alltom