2014-02-26 25 views
11

我有一個問題,我的SKView的背景色(灰色)在場景出現之前暫時顯示。無法設置SKView backgroundColor

我手動嘗試通過故事板編輯器和我的控制器(self.skView.backgroundColor = [SKColor blueColor])設置它,但它仍然是灰色的。這不是可以被覆蓋的屬性嗎?

更新#1

這裏的,以什麼發生一點解釋:

  1. viewDidLoad被調用,skView呈現在屏幕上W/A灰色背景(skView.scene尚未設定) 。
  2. 我加載了遊戲的所有資產(大約需要1秒),在此期間灰色背景可見。
  3. 資產已經被加載後,我加載場景,目前它(灰幕被替換瓦特/場景的內容)

的ViewController

- (void)viewDidLoad 
{ 
    [self.activityIndicator startAnimating]; 
    [self authenticatePlayer]; 

    self.skView.backgroundColor = [SKColor blueColor]; 

    // During this time the grey screen is visible as the assets are being loaded and take ~1 second 
    // self.skView.scene is NULL here so I cannot set the backgroundColor... 

    [GamePlayScene loadSceneAssetsWithCompletionHandler:^{ 
     [self.activityIndicator stopAnimating]; 
     self.activityIndicator.hidden = YES; 

     CGSize viewSize = self.skView.bounds.size; 

     self.gamePlayScene = [[GamePlayScene alloc] initWithSize:viewSize]; 
     self.adView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     [self.skView presentScene:self.gamePlayScene]; 

    }]; 
    ... 

回答

2

恐怕你要麼有做(比如設置的backgroundColor)最基本的設置來展示您的場景,然後加載休息,或存在的父節點一個「加載」場景,直到遊戲場景加載,然後替換它。

+0

問題是'self.skView.scene'在短時間內爲空(請參閱我更新的問題)。 –

+1

是的,就像GLKView一樣,SKView由CAEAGLLayer支持。這就是灰色來自的地方。改變這是可能的(這裏有一些答案),但我認爲這種情況有點矯枉過正。看看我更新的答案。 –

0

我想你它誤爲SKScene property of the same name

self.scene.backgroundColor = [SKColor blueColor]; 

注意self.scene(人所以self.view)將一直等到該節點已經被分別添加爲另一個節點(已經是場景圖的一部分)之後,直到該場景被呈現爲止。

+0

不是SKView只是從UIView(它有一個backgroundColor屬性)繼承? –

+0

此外,我已更新我的問題w /一些更多的細節。 –

+0

更多細節不需要,案例很清楚。您需要更改SKScene的backgroundColor屬性,而不是視圖的。該視圖具有相同的屬性,但場景是呈現(不透明)到視圖中的視圖,因此只有場景的背景色纔會生效。 – LearnCocos2D

0

在呈現視圖之前,您應該能夠在呈現視圖控制器viewDidLoad中設置SkView背景色。這沒有發生?

提供您已經將其添加到

0

使用的精靈套件的基本模板,我能夠將背景設置爲黑色此代碼GameViewController.m文件裏:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

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

// Create and configure the scene. 
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size]; 
scene.scaleMode = SKSceneScaleModeAspectFill; 
scene.backgroundColor = [UIColor blackColor]; 

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

不知道這是否是您要尋找的到底是什麼,但它擺脫了似乎沒有消失的灰色背景。在你的情況下,我認爲你想在初始化場景後設置「gamePlayScene」背景顏色屬性。

希望這會有所幫助!

+0

OP詢問SKView背景顏色,而不是SKScene –

+1

我明白這不是OP想要的,但它的工作原理可能是最簡單的解決方案。我認爲它不會產生任何不利影響,因此一方與另一方無關。 :) – johnnelm9r

相關問題