I want to make an SKView I can use as a factory to make SKShapeNodes and "render" them to textures.
But I can't find how I would initialise such a thing, and am having no luck, at all.
How do I make a standalone SKView for this purpose?
Or is there a better way to do this that avoids using the gamescene?
Here's my FUTILE Effort at making a factory, this complains that texture(from: ) is ambiguous. I have no idea what that means.
import SpriteKit
class Make: SKView{
static func circle() -> SKSpriteNode {
let myShapeNode = SKShapeNode(circleOfRadius: 100)
myShapeNode.fillColor = SKColor.lightGray
myShapeNode.strokeColor = SKColor.gray
let tex = texture(from: myShapeNode)
return SKSpriteNode(texture: tex)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Update
After more futile time on google, I tried searching for initialisation of a UIView, and found and added this piece of code, that initialises to a frame that seems imaginary... but it works! I don't know why... but I can't use it as a factory method, only as an instance method, this way:
import Foundation
import SpriteKit
class Make: SKView{
// added randomly found UIView initialisation "code"...
override init(frame: CGRect) {
super.init(frame: frame)
}
func circle() -> SKSpriteNode {
let myShapeNode = SKShapeNode(circleOfRadius: 100)
myShapeNode.fillColor = SKColor.lightGray
myShapeNode.strokeColor = SKColor.gray
let tex = texture(from: myShapeNode)
return SKSpriteNode(texture: tex)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…