如何按时间顺序移动数组?

How to move through an array chronologically?

我正在开发一款拖放游戏,用户可以在其中将标签移动到相应的节点。

我目前有这个代码:

func setupDragLabel(){
    let random = Int(arc4random_uniform(1))

    if random == 0 {
        let numbersArray = ["one", "two", "three", "four", "five"]
        let index = Int(arc4random_unifrom(UInt32(numbersArray.count)))
        label.text = numbersArray[index]
        label.name = "numbers"
        }
    else{
        let lettersArray = ["a", "b", "c", "d", "e"]
        let index = Int(arc4random_uniform(UInt32(lettersArray.count)))
        label.text = lettersArray[index]
        label.name = "letters"
    }
    ((self.addChild(label))
}

然后结束触摸:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if label.name == "numbers"{
        if numbersBin.frame.contains(label.position){
            label.removeFromParent()
            setupDragLabel()
        }
    }
    if label.name == "letters" {
        if lettersBin.frame.contains(label.position){
            label.removeFromParent()
            setupDragLabel)
        }
    }
}

目前,该函数会一直运行,从数组中随机选择。但是,我想对其进行设置,以便函数 setupDragLabel() 移动通过每个数组,当它到达两个数组的末尾时,它将移动到下一个屏幕。我将如何更改我当前的代码(我知道这意味着删除 arc4random,但不确定如何在顶部修复它),以及我需要编写什么函数才能让它继续?

我建议制作 numbersArraylettersArray 变量,然后从这些数组中删除您已经使用的元素。

var numbersArray = ["one", "two", "three", "four", "five"]
var lettersArray = ["a", "b", "c", "d", "e"]

func setupDragLabel(){
    let random = Int(arc4random_uniform(1))

    if random == 0, numbersArray.count > 0 {
        let index = Int(arc4random_uniform(UInt32(numbersArray.count)))
        label.text = numbersArray[index]
        numbersArray.remove(at: index)
        label.name = "numbers"
        self.addChild(label)
    }
    else if lettersArray.count > 0 {
        let index = Int(arc4random_uniform(UInt32(lettersArray.count)))
        label.text = lettersArray[index]
        label.name = "letters"
        self.addChild(label)
    }
}