2016-04-01 41 views
0

在我的SpriteKit遊戲的標題畫面中,我想要有一個SKLabelNode顯示「Play」。但是,當我運行下面的代碼時,節點不會添加到場景中。SKLabelNode未在SKScene中創建 - Swift

import UIKit 
import SpriteKit 

class TitleScreen: SKScene { 

var title: SKLabelNode! 

override func didMoveToView(view: SKView) { 

    backgroundColor = SKColor(colorLiteralRed: 0.4, green: 0.835, blue: 1, alpha: 1) 

    //The color is a light blue, so the node should not be hidden by the color of the screen. 

    title = SKLabelNode() 
    title.text = "Play" 
    title.color = SKColor.blackColor() 
    title.fontSize = 70 

    self.addChild(title) 

} 

我該怎麼做才能做到這一點?

+0

驗證didMoveToView被調用 – Knight0fDragon

回答

0

您沒有正確設置標籤的位置,所以它可能是在屏幕外。你的代碼適合我。試試這個:

override func didMoveToView(view: SKView) { 

     backgroundColor = SKColor(colorLiteralRed: 0.4, green: 0.835, blue: 1, alpha: 1) 

     //The color is a light blue, so the node should not be hidden by the color of the screen. 

     title = SKLabelNode() 
     title.position = CGPoint(x: frame.midX, y: frame.midY) 
     title.text = "Play" 
     title.color = SKColor.blackColor() 
     title.fontSize = 70 

     self.addChild(title) 
    } 

請注意,當從.sks文件加載場景時,它的默認大小爲1024x768。如果你想改變它,請在創建時明確設置場景的大小。如果你想匹配視圖的大小,你可以這樣做:

scene.size = yourSkView.bounds.size