2015-02-05 1 views
2

I 최고 점수 변수 이제SKLabelNode 텍스트에는 두 가지 글꼴과 색상이 있습니다. 이것이 어떻게 가능한지?

scoreLabel.text = "\(score)/\(classicHScoreInt)"

다음에 점수 변수를 표시하도록 설정하는 SKLabelNode이 모든 것을 잘 보여줍니다하지만 난 작은 글꼴 어쩌면 다른 색으로하는 classicHScoreInt 싶습니다있다 . 이것이 어떻게 가능한지?

classicHScoreInt는 (언급 한 바와 같이) 정수와 같은 SKLabelNode 인스턴스에 두 개의 글꼴을 설정할 수 없습니다 score

답변

6

그래서입니다. 대신 하위 클래스를 작성하여 다양한 글꼴 크기의 복수 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 클래스 외부에서 단일 SKNodeScoreLabel 행위에

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

두 개의 레이블을 사용. SKActionsScoreLabel에서 실행될 수 있으며 child label nodes에 모두 영향을 미칩니다. 예 :

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

이렇게하면 두 레이블을 하나의 단위로 함께 표시합니다.

+0

레이블 노드가 속성이 지정된 문자열을 지원하지 않습니까? – LearnCocos2D

+0

그럴까요? 나는 그렇게 생각하지 않는다. NSAttributedString에서 문자열 텍스처를 수동으로 생성하는 것보다이 프로세스가 더 쉽다고 생각했습니다. – rakeshbs

관련 문제