Rich
Rich

Reputation: 13

Resizing a SpriteKit Scene

Im trying to make the scene a different size based on what device the user is using and this method works great all the time except right when the app loads up. For example the app loads and the screen size is not correct but then right when I go to a new scene the size goes to what it is suppose to. Even if you go back to the starting scene it will be the correct size. It is only off when the app first loads and until you go to a new scene. Here is the code, any help would be appreciated.

- (void)viewDidLoad
{
[super viewDidLoad];

iphone4x = 1.2;
iphone4y = 1.4;
iphone5x = 1.1;
iphone5y = 1.18;


// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;

CGSize newSize;

if(iPhone4) {

    newSize = CGSizeMake(skView.bounds.size.width * iphone4x, skView.bounds.size.height * iphone4y);


}


if (iPhone5) {


    newSize = CGSizeMake(skView.bounds.size.width * iphone5x, skView.bounds.size.height * iphone5y);


}


if (iPhone6) {


    newSize = CGSizeMake(skView.bounds.size.width, skView.bounds.size.height);


}

if(iPhone6Plus) {





}





// Create and configure the scene.
SKScene *scene = [MainMenu sceneWithSize:newSize];
scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:scene];
}

Upvotes: 1

Views: 480

Answers (1)

Whirlwind
Whirlwind

Reputation: 13665

View is loaded in portrait orientation by default so probably the coordinates of the portrait mode will be used even if the app should run in landscape . This is kind a known behaviour. So maybe that's a problem if you run your app in landscape mode.

View’s size will be correct by the time the viewWillLayoutSubviews method is called.

Try using viewWillLayoutSubviews instead viewDidLoad:

 - (void)viewWillLayoutSubviews
    {

        [super viewWillLayoutSubviews];


        // Configure the view.
        SKView * skView = (SKView *)self.view;
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;
        skView.showsDrawCount = YES;
        //skView.showsQuadCount = YES;

        skView.showsPhysics = YES;
        skView.ignoresSiblingOrder = YES;

//you have to check if scene is initialised because viewWillLayoutSubviews can be called more than once
        if(!skView.scene){
            // Create and configure the scene.

            //instead of this from your code
           //SKScene *scene = [MainMenu sceneWithSize:newSize];

            //use this line
            MainMenu * scene = [MainMenu sceneWithSize:newSize];

            scene.scaleMode = SKSceneScaleModeAspectFill;

            // Present the scene.
            [skView presentScene:scene];
        }


}

Upvotes: 1

Related Questions