0

How can i make a rootnode of a scene spawn duplicates of itself in a position (For example: x: 10,y: 10,z: 10) then move in a direction until they are completely off screen and then dissappear.

I'm not asking for code, but i am asking for how to do this(For example what do i use for duplicating a rootnode?

How do i make that node disappear?

How do i generate a random number between 1 and 10 that will determine when to spawn the duplicate?)

And How do i make my post meet the quality standards?

Asphys
  • 175
  • 3
  • 15

2 Answers2

0

You'll want to use a generalized class of nodes and spawn instances randomly.

Here's an answer on how to make random numbers in Swift. Use that to make three different random numbers between 1 and 10, and use the node class constructor with the three random numbers as input.

Finally, to move the node in a random direction, you need to have some movement function in the node class and feed it random numbers.

This is how you ask a good question.

Community
  • 1
  • 1
AlgorithmsX
  • 307
  • 2
  • 10
0

Here is an answer:

You could:

  1. 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.

  2. Next assign the copy to a variable (so you can access it).

  3. Then add any actions if you need to (refer below).

  4. 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
  1. Then add any actions you need to (refer below).

  2. Finaly call scene.rootNode.addChildNode(theNodeYouJustPositioned)

  3. 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!

Community
  • 1
  • 1
Pro Blaster
  • 491
  • 3
  • 9