简体   繁体   中英

SKLabelNode upside down possible?

I'm developing a game in xcode 6 with swift. I have a scorelabel var scoreLabel: SKLabelNode! . Now in one of my methods I show my label:

scoreLabel = SKlabelNode(fontNamed: "TrebuchetMS-Bold")
scoreLabel.text = "\(score)"
scoreLabel.fontSize = 30
scoreLabel.fontColor = SKColor .redColor()
scoreLabel.position = CGPoint(x:780, y:180)
addChild(scoreLabel)

That shows my score as for example: 2500. Is it possible to show this upside down?

  • 2
  • 5
  • 0
  • 0

Sorry, I wrote this as list, because I wasn't able to write upside down here.

Thanks for answers.

Use the SKLabelNodePlus label for this.
Here is the link: https://github.com/MKargin0/SKLabelNodePlus

Then, just invert the label by writing myLabel.xScale = -1 . Its as simple as that!

You can create a custom class to loop through each character in the string and create a separate SKLabelNode for each character .

class SKVerticalLabelNode : SKNode
{
    var fontName : String = "" {
        didSet {
            self.updateLabelProperties()
        }
    }

    var fontSize : CGFloat = 10.0 {
        didSet {
            self.updateLabelProperties()
        }
    }

    var fontColor : UIColor = UIColor.whiteColor() {
        didSet {
            self.updateLabelProperties()
        }
    }

    var text : String = "" {
        didSet {
           self.updateLabelProperties()
        }
    }

    func updateLabelProperties () {
        self.removeAllChildren()
        let length = countElements(text)
        var yPosition : CGFloat = 0
        for character in reverse(text) {
            let label = SKLabelNode(fontNamed: self.fontName)
            label.fontColor = self.fontColor
            label.fontSize = self.fontSize
            label.text = String(character)
            label.position = CGPointMake(0, yPosition)
            yPosition += label.frame.size.height
            addChild(label)
        }
    }
}

It can be used like this

let scoreLabel = SKVerticalLabelNode()
scoreLabel.fontName = "TrebuchetMS-Bold"
scoreLabel.text = "2500"
scoreLabel.fontSize = 30
scoreLabel.fontColor = UIColor.redColor()
scoreLabel.position = CGPoint(x:180, y:180)
addChild(scoreLabel)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM