Home > Software design >  How do I give an SKShapeNode a color?
How do I give an SKShapeNode a color?

Time:01-02

I want to create an SKShapeNode and I want to add a stroke color to it. But when I try it does not work and I keep on receiving this error:

Consecutive declarations on a line must be separated by ';'

Here is my code:

import SpriteKit

class Lable: SKLabelNode {
    override init() {
        super.init()
        
        text = String(1)
        
        fontSize = 128
        fontName = "Futura Bold"
        fontColor = .white
        
        position = CGPoint(x: -8, y: -50)
        zPosition = 4
    }
    
    func updateLable(level : Int) {
        text = String(level)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class LableBackground: SKNode {
    
    let ring = SKShapeNode(circleOfRadius: 100)
    ring.strokeColor = .lightGray //ERROR ON THIS LINE
    ring.alpha = 0.5
    ring.lineWidth = 30
    ring.zPosition = 0
    
    //let bg = SKShapeNode(circleOfRadius: 100)
    //bg.fillColor =
    
    override init(){
        super.init()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

How do I fix this problem and what did I do wrong?

Note: This is not a finished program so it might contain comments/messy code.

I tried doing ring.strokeColor = .lightGray to give my SKShapeNode a stroke color and it just gives an error every time.

CodePudding user response:

You should make the code like this:

import SpriteKit


class Lable: SKLabelNode {
override init() {
    super.init()
    
    text = String(1)
    
    fontSize = 128
    fontName = "Futura Bold"
    fontColor = .white
    
    position = CGPoint(x: -8, y: -50)
    zPosition = 4
}

func updateLable(level : Int) {
    text = String(level)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
}

class LableBackground: SKNode {


override init(){
    super.init()
    let ring = SKShapeNode(circleOfRadius: 100)
    ring.strokeColor = .lightGray //ERROR ON THIS LINE
    ring.alpha = 0.5
    ring.lineWidth = 30
    ring.zPosition = 0

    //let bg = SKShapeNode(circleOfRadius: 100)
    //bg.fillColor =

    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
  • Related