SKLabelNode text with two different fonts and colour. How is this possible?

本小妞迷上赌 提交于 2019-12-06 09:40:30

You cannot set two fonts to the same SKLabelNode instance. Instead you can write subclasses to create a custom node which contains multiple SKLabelNodes with different font sizes. For example, Your scoreLabel can be an instance of the following class.

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)
    }

}

Using ScoreLabel class

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

The two labels in ScoreLabel acts as a single SKNode from the outside. SKActions can be executed on the ScoreLabel and it will affect both the child label nodes. For instance

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

This will scale both labels together as a single unit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!