SKScene iPad screen height canceled

I am trying to fill my SKScene tiles in an iPad application that only supports landscape mode. Inside the scene, I find h and w as such:

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

Unfortunately, dimensions sent back are the opposite of what they need. SO discusses this issue in the contents of the view or view controller, and the solution seems to be to detect screen size in viewWillAppear, not viewDidLoad. However, this does not seem to be a valid solution for SKScenes.

Any suggestions?

+6
source share
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]; } } 
+12
source

It does not make sense to initialize the scene in the layout handler. Resize the scene in the layout handler. (What is this 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 to the right place.

Hooray, now your game also handles the rotation of the device.

+10
source

Source: https://habr.com/ru/post/955890/


All Articles