2015-02-12 54 views
1

我试图根据用户使用的设备使场景变成不同的大小,并且除了应用程序加载时的权利之外,此方法始终工作得很好。例如,应用程序加载和屏幕大小不正确,但是当我去到一个新的场景时,大小达到了它的设定。即使你回到起始场景,它也会是正确的大小。只有在应用程序第一次加载时才会关闭,直到您进入新场景。这里的代码,任何帮助将不胜感激。调整SpriteKit场景的大小

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

您使用的是相同的代码来呈现其它场景? – rakeshbs 2015-02-12 01:12:35

+0

使用viewWillLayoutSubviews并考虑有scene.scaleMode – LearnCocos2D 2015-02-13 08:09:25

回答

1

默认情况下纵向加载视图,所以纵向模式的坐标可能会被使用,即使应用程序应该在横向上运行。这是一种已知的行为。所以,如果您在横向模式下运行应用程序,那么这可能是个问题。

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

使用try 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]; 
     } 


}