NC_DEV
NC_DEV

Reputation: 45

How to use properties from SCNFloor with Swift and Scenekit?

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 :)

Upvotes: 5

Views: 4227

Answers (2)

fqdn
fqdn

Reputation: 2843

(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

Upvotes: 3

rickster
rickster

Reputation: 126137

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)

Upvotes: 9

Related Questions