2015-02-05 72 views

回答

6

您不能同時設置兩個字體相同的SKLabelNode實例。相反,您可以編寫子類來創建包含多個不同字體大小的多個SKLabelNodes的自定義節點。例如,您的scoreLabel可以是以下類的一個實例。

class ScoreLabel : SKNode 
{ 
    var label : SKLabelNode! 
    var scoreLabel : SKLabelNode! 

    var score : Int = 0 { 
     didSet 
     { 
      scoreLabel.text = "\(score)" 
     } 
    } 

    override init() { 
     super.init() 
     label = SKLabelNode(text: "Score : ") 
     label.position = CGPointMake(0, 0) 
     label.fontSize = 20 
     addChild(label) 

     scoreLabel = SKLabelNode(text: "\(0)") 
     scoreLabel.position = CGPointMake(label.frame.size.width , 0) 
     scoreLabel.fontSize = 25 
     addChild(scoreLabel) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

} 

使用ScoreLabel

let scoreLabel = ScoreLabel() 
scoreLabel.position = CGPointMake(100, 300) 
scoreLabel.score = 10 
self.addChild(scoreLabel) 

ScoreLabel行爲的兩個標籤作爲來自外部的單個SKNodeSKActions可以在ScoreLabel上執行,它會影響到child label nodes。例如,

scoreLabel.runAction(SKAction.scaleTo(2.0, duration: 2.0)) 

這將把兩個標籤放在一起作爲一個單元。

+0

沒有標籤節點支持屬性字符串? – LearnCocos2D 2015-02-06 08:08:38

+0

做到了嗎?我不這麼認爲。我認爲這個過程比手動從NSAttributedString生成字符串紋理更簡單 – rakeshbs 2015-02-06 08:09:36

相關問題