简体   繁体   中英

How to change screen size based on device in Sprite Kit?

I noticed a problem that I didn't have before - on iPhone 5 and related devices my game shows black zones on top and bottom.

I use standard code for it and it shows that skView.bounds.size is 320x480 even on big devices.

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

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

    if (!skView.scene) {
        NSLog(@"size: %@", NSStringFromCGSize(self.view.frame.size));
        SKScene * scene = [MainMenuScene sceneWithSize:skView.bounds.size]; //[MyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;
        [skView presentScene:scene];
    }
}

EDIT: It determines iPad sizes correctly.

EDIT2: changing scene size does not increase its height. I tried setting to 320, 568 manually to no avail.

EDIT3: it shows same black stripes on default Xcode project. Something is very wrong here.

Here is a screenshot:

在此输入图像描述

Somehow Xcode did not provide Default launch images with the project. After I added launch images it all started working.

In https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html , The documentation says 'To let the system know that your app supports the iPhone 6 screen sizes, include a storyboard launch screen file in your app's bundle. At runtime, the system looks for a storyboard launch screen file. If such an file is present, the system assumes that your app supports the iPhone 6 and 6 Plus explicitly and runs it in fullscreen mode. If such an image is not present, the system reports a smaller screen size (either 320 by 480 points or 320 by 568 points) so that your app's screen-based calculations continue to be correct. The contents are then scaled to fit the larger screen.'.

I solved by selecting 'Project'->'App Icons and Launch Images'->'Launch Image source' and then let Xcode produce the file. It is possible to copy this file in from an older project as well. (project made on Xcode 5.1.1 or previous).

This also happens when creating a project for iOS 8+ in xCode 6 and then try and execute the application on iOS 7 or less.

To change the view size of Your scene you can do this

   SKView * skView = (SKView *)self.view;
   CGSize viewSize = self.view.bounds.size;
   viewSize.height *= 2;
   viewSize.width *= 2;
   SKScene * scene = [MainMenuScene sceneWithSize:viewSize];
   scene.scaleMode = SKSceneScaleModeAspectFill;

The scene viewSize will be scale by 2 here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM