I've created a SpriteKit Scene file and the corresponding SKScene object. That scene object includes an @Binding property.
When I use SpriteView in my SwiftUI file, I can't figure out how to initialize the scene so that it loads from the sks file and also assigns a binding.
I'm wanting something like this:
class MyScene: SKScene {
@Binding var foo: CGFloat
init(foo: Binding<CGFloat>) {
_foo = foo
super.init(fileNamed: "MyScene")
}
}
That doesn't work though because init(fileNamed:) is a convenience initializer, not a designated initializer.
A possible workaround using
ObservableObjectto subscribe tofoo. I pass a fileName so I have an excuse to invoke my convenience initializer.And then hold a reference to it on your view (or elsewhere).