Adding anchors at random locations
We want to create a new anchor node randomly every few seconds, and by using the crosshair we should be able to center the crosshair on the virtual 3D object and tap the screen to remove the virtual object from the scene. We will then track the score to see how many virtual objects are left in the scene.
At the top of the Scene
class, create two new variables of type timeInterval
and Int
called creationTime
and score
, and initialize both of them to 0
:
var creationTime: TimeInterval = 0 var score: Int = 0
Also add a new function called randomFloat
, which will take in a minimum and maximum float value and generate a random float value between the maximum and minimum values:
func randomFloat(min: Float, max: Float) -> Float { return (Float(arc4random()) / 0xFFFFFFFF) * (max - min) + min }
Create another function called createAnchorNode
. In this function, we will create an anchor node in the scene:
func createAnchorNode(){ ...