Sprite kit 调整SpriteKit场景的大小

Sprite kit 调整SpriteKit场景的大小,sprite-kit,skscene,Sprite Kit,Skscene,我正在尝试根据用户使用的设备将场景设置为不同的大小,这种方法一直都很有效,但应用程序加载时除外。例如,应用程序加载,屏幕大小不正确,但当我进入一个新场景时,屏幕大小与预期大小一致。即使返回到起始场景,它的大小也是正确的。只有在应用程序首次加载时,并且在您转到新场景之前,它才会关闭。这是代码,任何帮助都将不胜感激 - (void)viewDidLoad { [super viewDidLoad]; iphone4x = 1.2; iphone4y = 1.4; iphone5x = 1.1; i

我正在尝试根据用户使用的设备将场景设置为不同的大小,这种方法一直都很有效,但应用程序加载时除外。例如,应用程序加载,屏幕大小不正确,但当我进入一个新场景时,屏幕大小与预期大小一致。即使返回到起始场景,它的大小也是正确的。只有在应用程序首次加载时,并且在您转到新场景之前,它才会关闭。这是代码,任何帮助都将不胜感激

- (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];
}

默认情况下,视图以纵向加载,因此即使应用程序应在横向运行,也可能会使用纵向模式的坐标。这是一种众所周知的行为。所以,如果你在横向模式下运行你的应用程序,这可能是个问题

调用viewWillLayoutSubviews方法时,视图的大小将是正确的

尝试使用viewWillLayoutSubviews代替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];
        }


}

是否使用相同的代码呈现其他场景?使用VIEW WayLayOutSubVIEW并考虑存在ScNe.SimeMeMod