swift - Creating a subclass of a SCNNode -
i'm loading node .dae file following code:
func newnode() -> scnnode { var node = scnnode() let scene = scnscene(named: "circle.dae") var nodearray = scene!.rootnode.childnodes childnode in nodearray { node.addchildnode(childnode as! scnnode) } return node }
i add properties , methods specific node when load in scene gets random color can modify whenever want. had done similar subclass of scnsphere (which geometry , not node, though) using:
let numberofcolors: uint32 = 4 enum ellipsoidcolor: int, printable { case red = 0, blue, green, white var ellipsoidname: string { switch self { case .red: return "red" case .blue: return "blue" case .green: return "green" case .white: return "white" } } var description: string { return self.ellipsoidname } static func random() -> ellipsoidcolor { return ellipsoidcolor(rawvalue: int(arc4random_uniform(numberofcolors - 1)))! } } class ellipsoid: scnnode { func setmaterialcolor(ellipsoidcolor: ellipsoidcolor) { let color: uicolor switch ellipsoidcolor { case .red: color = uicolor.redcolor() case .blue: color = uicolor.bluecolor() case .green: color = uicolor.greencolor() case .white: color = uicolor.whitecolor() } self.geometry?.firstmaterial!.diffuse.contents = color } var color : ellipsoidcolor { didset { self.setmaterialcolor(color) } } init(color: ellipsoidcolor) { self.color = color super.init() self.setmaterialcolor(color) } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
how can "link" subclass node obtain using newnode() ? naively thought using
let ellipsoid = newnode() as! ellipsoid
would work, doesn't. time , help.
well, code cannot work.
in newnode
create scnnode
:
func newnode() -> scnnode { var node = scnnode() //... return node }
and later on tell compiler scnnode
ellipsoid
let ellipsoid = newnode() as! ellipsoid
but isn't! it’s scnnode
. of course program crash.
if want ellipsoid
create one:
func newellipsoid() -> ellipsoid { var node = ellipsoid() //... return node }
(or wherever need create it)
Comments
Post a Comment