swiftsprite-kitsklabelnode

How to display 2 labels in one scene?


I am totally new with Swift and SpriteKit, apologies I really know nothing. I am trying to add 2 consecutive SKLabelNode in a single scene. The background of the scene is .black. I created a function that generates a SKShapeNode from a UIBezierPath to cover let's say 3/4 of the screen in a sort of P shape, .fill = .white. I add the child and end the function.

I add the function in the didMove override. (Don't ask me why, I am following some examples and for the moment it seems to work. I will address this later, but at the moment I'm focused on the current problem)

Then I create a second function to add 2 SKLabelNode. One black over the white portion and one white over the black portion. I then add both to the scene.

I add the function after the shape one.

The result is that the black over the white portion is not displayed. If I comment the white over the black, the previous is displayed without issues.

I tried to infer the order of the addChild but the outcome doesn't change.

Does anyone know the reason?

I tried switching the addChild.

import SpriteKit

class MenuScene: SKScene {

override func didMove(to view: SKView) {
    layoutScene()
    addLabels()
}

func layoutScene() {
    backgroundColor = .black
    let rectSize = frame.size.width / 4
    //Codice per creare la scena della home
    //inizio
    let homeScreen = SKShapeNode()
    homeScreen.fillColor = .white
    let shapeHome = UIBezierPath()
    let shapeOrigin = CGPoint(x: view!.bounds.minX, y: view!.bounds.minY)
    shapeHome.move(to: shapeOrigin)
    shapeHome.addLine(to: CGPoint(x: view!.bounds.minX, y: view!.bounds.maxY))
    shapeHome.addLine(to: CGPoint(x: view!.bounds.maxX, y: view!.bounds.maxY))
    shapeHome.addLine(to: CGPoint(x: view!.bounds.maxX, y: rectSize))
    shapeHome.addLine(to: CGPoint(x: view!.bounds.maxX - rectSize, y: rectSize))
    shapeHome.addLine(to: CGPoint(x: view!.bounds.maxX - rectSize, y: view!.bounds.minY))
    shapeHome.addLine(to: CGPoint(x: view!.bounds.minX, y: view!.bounds.minY))
    shapeHome.close()
    homeScreen.path = shapeHome.cgPath
    homeScreen.fillColor = .white
    //fine
    addChild(homeScreen)
}

func addLabels() {
    let rectSize = frame.size.width / 4
    let home = SKLabelNode(text: "HOME")
    home.fontName = "AvenirNext-Bold"
    home.fontSize = 50.0
    home.fontColor = .black
    home.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(home)

    let b = SKLabelNode(text: "B")
    b.fontName = "AvenirNext-Bold"
    b.fontSize = 40.0
    b.fontColor = .white
    b.position = CGPoint(x: frame.maxX - (rectSize/2), y: rectSize/2)
    addChild(b)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    }
}

Solution

  • Thanks for helping.

    I just solved the issue substituting the colours .black and .white with UIColor.black and UIColor.white.

    DOes anyone know the reason though?