Here is an answer:
You could:
First copy the rootnode using "node.clone" or "node.copy".
Clone duplicates the whole node tree (the node and children) whereas copy duplicates just that node.
Next assign the copy to a variable (so you can access it).
Then add any actions if you need to (refer below).
Then in your spawning loop or whatever you use to spawn objects, make a clone of the clone (the variable we made in step 2) and set it's position
, with a random number.
To generate a random number that can be positive or negative use something like:
((((Float(arc4random()) / Float(UINT32_MAX))*2)-1)*5)//five is the important
//number here because it means you will get a number ranging from
// -5 to 5
Then add any actions you need to (refer below).
Finaly call scene.rootNode.addChildNode(theNodeYouJustPositioned)
To remove a node call node.removeFromParentNode
.
Actions:
Alternitavely, to deleting the node yourself, and positioning the node yourself in a loop or such, and moving the node yourself, you could use SCNAction
.
To use SCNAction
simply call node.runAction(SCNAction)
.
There are many different ways of using SCNAction
so please visit the docs for a whole list.
To make your node position itself (sort of) try running a runblock:^node:SCNNode{}
and setting the node's position to a random number in there.
The node now positions it self so you can skip step 4.
Now if you want to move the node by a fixed direction (I mean "not random") you could just add a moveBy:xyz
action in step 3.
But if it had to be a random direction you could do this action in step 5 and feed random numbers into it.
To delete you nodes using a time delay you could use a sequence:
action and feed in an array containing, first, a waitForDuration:Seconds
action, and last, a removeFromParentNode
action.
Please visit the docs for SCNAction
and any other class I mentioned that you may not have understood.
I don't know swift (at all) so I am sure there are a few syntax errors in the code. (sorry)
Post comments below if your confused.
Thanks!