1

I am trying to create a label on my SKScene but it is not appearing. I don't know what is wrong. The code seems perfectly fine:

class end: SKScene {

    var label = UILabel()

    override func didMoveToView(view: SKView) {
        scene?.backgroundColor = UIColor(red: CGFloat(59.0/255.0), green: CGFloat(89.0/255.0), blue: CGFloat(152.0/255.0), alpha: CGFloat(1.0))

         label.text = "Game Over!"
         label.backgroundColor = UIColor.blackColor()
         label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width/3, height: 30))
         label.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width/7)
         label.textColor = UIColor.whiteColor()
         self.view?.addSubview(label)      
    }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Osaama Shehzad
  • 147
  • 1
  • 2
  • 12
  • how do you know it is not appearing? first, what is .center? That is not a normal UILabel property from what I can tell – Knight0fDragon Aug 08 '16 at 17:26
  • n/m center is a member of UIVIew, so the issue now is what are the coordinates. I am going to assume you are using auto layout, and at the time that didMoveToView is called, you are working in default coordinates, then autolayout takes over moving the view to outside of some forseeable scope perhaps – Knight0fDragon Aug 08 '16 at 17:32

2 Answers2

1

The problem is you have set textColor and backgroundColor both whiteColor(), change any one will show your text properly.

Edit:

I think you want to add the label inside view that you are passing as a parameter than use view.addSubview(label) instead of self.view?.addSubview(label).

Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

In Sprite-kit you should use SKLabelNode in your scene instead of add a UIKit label directly to your view because this could cause bad performance to your game.

I make an example:

let titleLabel = SKLabelNode(fontNamed:"HelveticaNeue-Bold")
titleLabel.text = String("Game Over!")
titleLabel.fontColor = SKColor.whiteColor()
titleLabel.position = CGPoint(x: size.width/2.0, y: size.height/2)
self.addChild(titleLabel)

You can find SKLabelNode also in the Sprite-kit Xcode demo project "hello, world":

enter image description here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133