swift 如何为SKShapeNode指定颜色(spritekit)

wljmcqd8  于 2023-01-01  发布在  Swift
关注(0)|答案(1)|浏览(103)

所以在xcode(spritekit)中,我想创建一个SKShapeNode,并想给它添加一个笔划颜色。但是当我尝试时,它不起作用,我不断收到这个错误:行上的连续声明必须用"“分隔;下面是我的代码:

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")
    }
}

我该如何解决这个问题?我做错了什么?
注意:这不是一个完成的程序,因此它可能包含注解/混乱的代码
我试着用ring.strokeColor = .lightGray来给予我的SKShapeNode一个笔画颜色,但每次都出错。

bybem2ql

bybem2ql1#

您应该编写如下代码:

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")
    }
}

相关问题