Search code examples
swiftfor-looprandomsprite-kitdirection

Swift: Algorithm To Change A Random Number After Multiple Loops


I want to let the zombies move 30 times in one random direction and after that change the direction of every zombie individually. I have multiple zombies. All of them should NOT move in the same direction. I am sure I am missing something stupid. My code:

for zombie in Zombies{
    if(zombieCounter >= 30){
        for zombie in Zombies{
            direction = Int(arc4random_uniform(4))
            switch direction{
            case 0:
                zombie.position.y += step
                zombie.direction = 1
            case 1:
                zombie.position.y -= step
                zombie.direction = 2
            case 2:
                zombie.position.x -= step
                zombie.direction = 3
            case 3:
                zombie.position.x += step
                zombie.direction = 4
            default:()
            }
        }
        zombieCounter = 0
    }
}

zombieCounter += 1

Before I had this code:

if(zombieCounter >= 25){
    zombieCounter = 0
    for zombie in Zombies{
        let direction = arc4random_uniform(4)
        switch direction{
        case 0: zombie.position.y += stepZombie
        zombie.direction = 0
        case 1: zombie.position.y -= stepZombie
        zombie.direction = 1
        case 2: zombie.position.x -= stepZombie
        zombie.direction = 2
        case 3: zombie.position.x += stepZombie
        zombie.direction = 3
        default:()
        }
    }
}

It gets a random direction for each of the zombies and moves the zombie just once. What I want to do is to move the zombies multiple time in the same direction without a for-loop in the cases. The function should be called one time. The next time the direction should be the same like the first time. After 30 runs the direction should be randomly calculated for each of the zombies.

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if !gameOver{
            let step: CGFloat = 5
            let stepZombie: CGFloat = 20
            var zombieMovement = 20

            if(zombieCounter >= 25){
                zombieCounter = 0
                for zombie in Zombies{
                    let direction = arc4random_uniform(4)
                    switch direction{
                    case 0: zombie.position.y += stepZombie
                    zombie.direction = 0
                    case 1: zombie.position.y -= stepZombie
                    zombie.direction = 1
                    case 2: zombie.position.x -= stepZombie
                    zombie.direction = 2
                    case 3: zombie.position.x += stepZombie
                    zombie.direction = 3
                    default:()
                    }
                }
            }
            zombieCounter += 1
        }
    }
}

Solution

  • Lukas Köhl, we already established an enemy class for you in our chat from your last question you posted, so what we want to do is take advantage of that.

    Keep a counter for the amount of steps that you want each zombie to take, and set it as a property.

    override var moveCounter : Int  = 30
    {
        didSet{
           if(moveCounter == 0)
           {
             direction = Int(arc4random_uniform(4)) + 1
             moveCounter = 30
           }
        }
    }
    

    Then, after every step the zombie takes, we do zombie.moveCounter-- (Eventually I would like to help you get all of this into the class)