I can't figure out how to access properties from the Scenekit SCNFloor class using Swift, in objective-c they do it like this:
SCNNode*floor = [SCNNode node];
floor.geometry = [SCNFloor floor];
((SCNFloor*)floor.geometry).reflectionFalloffEnd = 10;
This creates the floor, how do i acces "reflectionFalloffEnd"?
let levelFloor = SCNNode()
levelFloor.geometry = SCNFloor()
Thanks
EDIT Thanks for the quick responses it works now :)
Whether you're in ObjC or Swift, for the compiler to be happy with you using SCNFloor
properties or methods on a reference, that reference needs to have the type SCNFloor
.
You can do that inline with a cast:
// ObjC
((SCNFloor*)floor.geometry).reflectionFalloffEnd = 10;
// Swift
(levelFloor.geometry as SCNFloor).reflectionFalloffEnd = 10
But that can get rather unwieldy, especially if you're going to set a bunch of properties on a geometry. I prefer to just use local variables (or constants, rather, since the references don't need to change):
let floor = SCNFloor()
floor.reflectionFalloffEnd = 10
floor.reflectivity = 0.5
let floorNode = SCNNode(geometry: floor)
floorNode.position = SCNVector3(x: 0, y: -10.0, z: 0)
scene.rootNode.addChildNode(floorNode)
(levelFloor.geometry as SCNFloor).reflectionFalloffEnd = 10
is how you would make that cast in Swift - you were almost there!
as an alternative - if you are going to need to access this property often you may end up with a bunch of as
casts in your code :) you may instead prefer to simply declare constants to hold those objects:
let levelFloor = SCNNode()
let levelFloorGeometry = SCNFloor()
levelFloor.geometry = levelFloorGeometry
levelFloorGeometry.reflectionFalloffEnd = 10
// and so on as this property of your SCNNode's geometry needs to change throughout your app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With