JeremyRaven
JeremyRaven

Reputation: 119

Instantiating class doesnt work - swift

I have a simple function call in my main class to create some instances from my Cube class but I cant seem to get my instances to be added to my scene. I tried returning self inside my Cube class but Swift wont let me do this inside init.

func addCubeLoop() {
    for var i = 0; i <= 0; ++i {
        cube = Cube(num: i, importedCube: importedCube1)
        cubeArray.append(cube)
        theScene.rootNode.addChildNode(cubeArray[i])
    }
}


class Cube: SCNNode {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(num: Int, importedCube: SCNNode) {
        let _scale: Float = 60
        let cube: SCNNode = importedCube.copy() as! SCNNode

        super.init()

        cube.scale = SCNVector3Make(_scale, _scale, _scale)
        let node = SCNNode()            
        node.addChildNode(cube)
        node.position = SCNVector3Make(5, 20, 3)
        let collisionBox = SCNBox(width: 5.0, height: 5.0, length: 5.0, chamferRadius: 0)
        node.physicsBody?.physicsShape = SCNPhysicsShape(geometry: collisionBox, options: nil)
        node.physicsBody = SCNPhysicsBody.dynamicBody()
        node.physicsBody?.mass = 0.1
        node.physicsBody?.restitution = 0.8
        node.physicsBody?.damping = 0.5
        node.name = "dice" + String(num)
        node.physicsBody?.allowsResting = true
    }
}

Upvotes: 1

Views: 84

Answers (1)

David R&#246;nnqvist
David R&#246;nnqvist

Reputation: 56625

The nodes created in the init of Cube are not added as child nodes of it.

I've simplified the your code below to illustrate the problem.

func addCubeLoop() {
    for /* loop */ {
        // 1. create cube
        cube = Cube(num: i, importedCube: importedCube1)
        // 6. add cube to the scene's root node
        theScene.rootNode.addChildNode(cubeArray[i])
    }
}


class Cube: SCNNode {
    init(importedCube: SCNNode) {
        // 2. copy importedCube 
        let cube: SCNNode = importedCube.copy() as! SCNNode
        // configure cube  
        // ...          

        // 3. create node
        let node = SCNNode() 
        // 4. add cube (the copy) to node
        node.addChildNode(cube)
        // configure node
        // ...

        // 5. End of init
    }
}

For each run through the loop, this is what happens.

  1. A new Cube instance is created, passing importedCube1

  2. In the Cube initializer, the imported cube argument is copied. The node "cube" is now a copy of the argument.

  3. Still in the initializer, a new node (called "node" is created).

  4. Still in the initializer, "cube" (the copy) is added to "node". At this point, cube is a child node of "node", but the Cube instance itself (which is a node) had no child nodes.

  5. Init completes.

  6. The newly created Cube instance is added to the scene's root node.

At this point there are four relevant nodes:

  • the root node,
  • the cube instance node
  • the node called "node"
  • the imported copy

The cube instance node is a child of the root node. The imported copy is a child of the "node" node. However, The "node" node doesn't have a parent.

The fix is to make sure that the all nodes are part of the hierarchy by adding the "node" node to self inside the Cube instance initializer.

Upvotes: 2

Related Questions