I want to animate 2 cubes forever until the user hits one of them. The animation is supposed to be similar to ∞.
These are my cubes in sceneView that I want to animate: 
I am trying to do this with nested animations, and this partially works. Now, since animations always wait until they end before starting a new one, it doesn't look smooth.
Before proceeding with the development of ∞ animation, I would like to know if there is a better (and actually working) way. This is my current code:
SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position = SCNVector3Make(3, 0, 0) cube2.position = SCNVector3Make(-3, 0, 0) println("log 0") SCNTransaction.setCompletionBlock { println("log 1") while (animationKey != 1) { //initially animationKey = 0 SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x += 1 cube1.position.y += 1 cube2.position.x += 1 cube2.position.y += 1 println("log 2") SCNTransaction.setCompletionBlock { SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x -= 1 cube1.position.y -= 1 cube2.position.x -= 1 cube2.position.y -= 1 println("log 3") SCNTransaction.setCompletionBlock { animationKey = 1 } SCNTransaction.commit() } println("log 4") SCNTransaction.commit() } } println("log 5") SCNTransaction.commit() println("log 6") }
The output shows the following things: log 0, log 5, log 6, log 1, log 2, log 4, log 2 ... and then it just continues alternating between log 4 and log 2 .
Can anybody help me?
Note
I thought a recursive approach would be better, so I replaced animationKey = 1 with a call to the method it is in ... didn't work anymore.
I got the animation to work once, but now it just stops after the very first animation is completed and completely ignores the second and third animation blocks ...
source share