2015-02-12 87 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]; 
     } 


}