2015-02-05 37 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

相关问题