iosswiftsprite-kitibinspectable

custom SKSpriteNode classes for use in .sks file?


I'm making a custom SKSpriteNode Class, which I want to use in my .sks file. I want it to have @IBInspectable property. Is it possible? And how can I implement its init(coder:) method, or there is no need to implement it?


Solution

  • no it's not possible to use @IBInspectable, that only works with classes in the storyboard editor.

    you can create your custom class with an init and instantiate it from code. If you want to instatiate your custom object from the Scene editor you MUST use the init(coder:) func

    you can have both init's in your class in case you wish to instantiate your object in code at some point as well as creating in a scene sks file.

    init() {
    
        super.init(texture: nil, color: .clear, size, CGSize.zero)
    
        setup()
    }
    
    required init?(code aDecoder: NSCoder) {
    
        super.init(code: aDecoder)
    
        setup()
    }
    
    func setup() {
        //add some setup code here
    }
    

    Or if you ONLY want to instantiate the object in the scene file you can eliminate the normal init().