0

I am trying to create a genetic algorithm for running race cars around a race track. Each car gets random instructions that apply a force to the car and rotate the car by a certain number of degrees. In order to space out the new instructions given to each car, I used a delay time in dispatch Queue that adds 0.2 seconds to the previous instruction. e.g.

0 seconds - first instruction
0.2 seconds - second instruction
0.4 seconds -third instruction

and so on...

The problem I have is that after several instructions have been carried out I start to notice a longer delay between instructions, to the point where a new instruction is applied after say 2 seconds.

Here is my code below.

func carAction(newCar: [[CGFloat]], racecar: SKSpriteNode) {
        var caralive = true
        let max = 1000
        var count = 0
        let delayTime = 200000000
        var deadlineTime = DispatchTime.now()
        while count < max {
            let angleChange = newCar[count][1]
            let speedChange = newCar[count][0]
            count += 1
            deadlineTime = deadlineTime + .nanoseconds(delayTime)
            DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
                if caralive == true {
                    print(DispatchQueue.main)
                    racecar.physicsBody?.angularVelocity = 0
                    let rotate = SKAction.rotate(byAngle: (angleChange * .pi / 180), duration: 0.2)
                    racecar.run(rotate)
                    let racecarRotation : CGFloat = racecar.zRotation
                    var calcRotation : Float = Float(racecarRotation) + Float(M_PI_2)
                    let Vx = speedChange * CGFloat(cosf(calcRotation))
                    let Vy = speedChange * CGFloat(sinf(calcRotation))
                    let force = SKAction.applyForce(CGVector(dx: Vx, dy: Vy), duration: 0.2)
                    racecar.run(force)
                    let total = self.outerTrack.count
                    var initial = 0
                    while initial < total {
                        if racecar.intersects(self.outerTrack[initial]) {
                            racecar.removeFromParent()
                            self.numberOfCars -= 1
                            initial += 1
                            caralive = false
                            break
                        } else {
                            initial += 1
                        }
                    }
                } else {
    //                print(self.numberOfCars)
                }
            }
        }

The 2D array newCar is a list of all the instructions.

Any help would be massively appreciated as I have been trying to figure this out for ages now!!

Many thanks in advance, any questions just feel free to ask!

Bushers
  • 5
  • 4

1 Answers1

1

You should do something like this instead:

func scheduledTimerWithTimeInterval(){
    // Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("moveCarsFunction"), userInfo: nil, repeats: true)
}

And call scheduledTimerWithInterval once

originally answered here

Jake
  • 2,126
  • 1
  • 10
  • 23