Swift: 多次循环后改变随机数的算法

Swift: Algorithm To Change A Random Number After Multiple Loops

我想让僵尸在一个随机方向上移动 30 次,然后分别改变每个僵尸的方向。我有多个僵尸。他们都不应该朝同一个方向移动。我确定我错过了一些愚蠢的东西。我的代码:

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

在我有这段代码之前:

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:()
        }
    }
}

它为每个僵尸获取一个随机方向,并只移动一次僵尸。我想要做的是在没有 for-loop 的情况下多次移动僵尸在同一个方向。该函数应该被调用一次。下一次的方向应该和第一次一样。运行 30 次后,应该为每个僵尸随机计算方向。

    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
        }
    }
}

这个怎么样?

class Zombie {
    var position = CGPoint()
    var direction: UInt32 = 0
}

let maxSteps = 25
var Zombies = [Zombie]()
var gameOver = false
var stepsInCurrentDirection = MaxSteps

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

        let stepZombie: CGFloat = 20
        let changeDirection = stepsInCurrentDirection >= maxSteps

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

        if changeDirection { stepsInCurrentDirection = 1 }
        else { stepsInCurrentDirection += 1 }
    }
}

Lukas Köhl,根据您发布的上一个问题,我们已经在我们的聊天中为您建立了一个敌人 class,因此我们要做的就是利用它。

为您希望每个僵尸走的步数保留一个计数器,并将其设置为 属性。

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

然后,僵尸每走一步,我们就zombie.moveCounter-- (最后我想帮你把这一切都放进class)