Creating a subclass of SKShapeNode

2019-02-19 23:35发布

问题:

class ColorRectangle: SKShapeNode {

    var width: CGFloat!
    var height: CGFloat!
    var rectColor: UIColor!

    convenience init(rectOfSize: CGSize, colorOfRectangle rectColor: UIColor) {

        super.init(rectOfSize: rectOfSize)

        self.width = rectOfSize.width
        self.height = rectOfSize.height
        self.rectColor = rectColor

        self.fillColor = rectColor
        self.strokeColor = rectColor
    }

}

Can't get the initializer right. What do I do?

回答1:

Try changing your initializer like this:

class ColorRectangle: SKShapeNode {

    var width: CGFloat!
    var height: CGFloat!
    var rectColor: UIColor!

    convenience init(rectOfSize: CGSize, colorOfRectangle rectColor: UIColor) {

        self.init()

        self.init(rectOfSize: rectOfSize)

        self.rectColor = rectColor

        self.fillColor = rectColor
        self.strokeColor = rectColor
    }

}