move sphere animation - SceneKit Swift Xcode

162 views
Skip to first unread message

Bjorn Chin Fo Sieeuw

unread,
Jan 12, 2015, 1:35:52 PM1/12/15
to swift-l...@googlegroups.com

I want to make an animation in SceneKit in which the sphere is moving. I have the following code in Atoms.swift:

 

import Foundation

import SceneKit

 

class Atoms {

 

    class func carbonAtom()->SCNGeometry {

        var rootNode: SCNNode

        let carbonAtom = SCNSphere(radius: 1.70)

 

        carbonAtom.firstMaterial!.diffuse.contents = UIColor.darkGrayColor()

        carbonAtom.firstMaterial!.specular.contents = UIColor.whiteColor()

 

        let carbonNode = SCNNode(geometry: carbonAtom)

        self.rootNode.addChildNode(carbonNode)//error

        let moveUp = SCNAction.moveByX(0.0, y: 1.0, z: 0.0, duration: 1.0)

        carbonNode.runAction(moveUp)

 

        return carbonAtom

 

    }

 

}

The error in the line starting with self is:

'Atoms.Type' does not have a member named 'rootNode'. What does this mean and what do I have to do to make the sphere move?

Jens Alfke

unread,
Jan 12, 2015, 2:35:00 PM1/12/15
to Bjorn Chin Fo Sieeuw, swift-l...@googlegroups.com

On Jan 12, 2015, at 10:35 AM, Bjorn Chin Fo Sieeuw <bchinf...@gmail.com> wrote:

'Atoms.Type' does not have a member named 'rootNode'. What does this mean and what do I have to do to make the sphere move?

It says your Atoms class doesn't have a rootNode method or property. 

You defined a local variable named rootNode in the carbonAtom() method. So either you should refer to it as just "rootNode", or you should move the declaration outside the method declaration so it becomes a property. (But even if it's a property, you can't refer to it from inside a class method, because there's no instance.)

—Jens

Bjorn Chin Fo Sieeuw

unread,
Jan 12, 2015, 2:55:26 PM1/12/15
to swift-l...@googlegroups.com
Now I tried

var rootNode: SCNNode  = SCNNode()

as a local variable, and it compiles. But the sphere does not move. I am trying to modify the code found here:

https://www.dropbox.com/sh/nt0hnw1tib0ppkh/AAAJMAkUQcck2F-r3VUKDd7Xa?dl=0


Op maandag 12 januari 2015 19:35:52 UTC+1 schreef Bjorn Chin Fo Sieeuw:

Bjorn Chin Fo Sieeuw

unread,
Jan 12, 2015, 2:56:57 PM1/12/15
to swift-l...@googlegroups.com
Now I tried

var rootNode: SCNNode  = SCNNode()

as a local variable, and it compiles. But the sphere does not move. I am trying to modify the code found here:

https://www.dropbox.com/sh/nt0hnw1tib0ppkh/AAAJMAkUQcck2F-r3VUKDd7Xa?dl=0


Op maandag 12 januari 2015 19:35:52 UTC+1 schreef Bjorn Chin Fo Sieeuw:

I want to make an animation in SceneKit in which the sphere is moving. I have the following code in Atoms.swift:

Jens Alfke

unread,
Jan 12, 2015, 2:59:22 PM1/12/15
to Bjorn Chin Fo Sieeuw, swift-l...@googlegroups.com
The code you're modifying is just supposed to create the scene nodes. There must be other code in the project that actually runs the animation. You can't run the animation from the file you're modifying because it doesn't know what scene the atoms are going to be added to.

—Jens
Reply all
Reply to author
Forward
0 new messages