为什么我的SKSpriteNode随机覆盖标签?

时间:2018-08-20 19:52:14

标签: arrays swift skspritenode arc4random sklabelnode

我正在关注this tutorial中的拖放游戏。我在标签后面添加了一个便条纸的SKSpriteNode图像,作为白色背景。

有时(通常是游戏三到四次),便条卡会变成空白。但是,我知道标签仍附加在SKSpriteNode上,因为我仍然可以玩游戏-我只是看不到标签。并非每次都来自相同的数组或数组中的相同文本,尽管有些数组比其他数组更经常出现空白。代码中是否存在导致此问题的内容?还是完全随机?

func setupDragLabel(){
    //set the font and position of the label
    label.fontName = "Flama"
    label.fontSize = 20
    label.fontColor = UIColor.black
    label.preferredMaxLayoutWidth = 225
    label.lineBreakMode = .byWordWrapping
    label.numberOfLines = 0
    label.zPosition = 3
    label.position = CGPoint (x: self.frame.midX, y: (self.frame.midY-150))

    //get a random integer between 0 and 2
    let random = Int(arc4random_uniform(3))

    //depending on the random number set the text and name to either yellow, red, or green
    if random == 0, yellowArray.count > 0
    {
        let index = Int(arc4random_uniform(UInt32(yellowArray.count)))
        label.text = yellowArray[index]
        yellowArray.remove(at: index)
        label.name = "yellow"
        (self.addChild(label))

    }else if random == 1, redArray.count > 0
    {
        let index = Int(arc4random_uniform(UInt32(redArray.count)))
        label.text = redArray[index]
        redArray.remove(at: index)
        label.name = "red"
        (self.addChild(label))
    }else if greenArray.count > 0
    {
        let index = Int(arc4random_uniform(UInt32(greenArray.count)))
        label.text = greenArray[index]
        greenArray.remove(at: index)
        label.name = "green"
        (self.addChild(label))

    }else if greenArray.count == 0
    {
        let random = Int(arc4random_uniform(2))

        if random == 0, yellowArray.count > 0
        {
            let index = Int(arc4random_uniform(UInt32(yellowArray.count)))
            label.text = yellowArray[index]
            yellowArray.remove(at: index)
            label.name = "yellow"
            (self.addChild(label))
        }
        else if random == 1, redArray.count > 0
        {
            let index = Int(arc4random_uniform(UInt32(redArray.count)))
            label.text = redArray[index]
            redArray.remove(at: index)
            label.name = "red"
            (self.addChild(label))
            }
        }else if random == 1, redArray.count == 0
    {
        if yellowArray.count > 0
        {
            let index = Int(arc4random_uniform(UInt32(yellowArray.count)))
            label.text = yellowArray[index]
            yellowArray.remove(at: index)
            label.name = "yellow"
            (self.addChild(label))
        }
        else if greenArray.count > 0
        {
            let index = Int(arc4random_uniform(UInt32(greenArray.count)))
            label.text = greenArray[index]
            redArray.remove(at: index)
            label.name = "green"
            (self.addChild(label))
        }
        }
        else if random == 0, yellowArray.count == 0
    {
        if redArray.count > 0
        {
            let index = Int(arc4random_uniform(UInt32(redArray.count)))
            label.text = redArray[index]
            redArray.remove(at: index)
            label.name = "red"
            (self.addChild(label))
        }
    else if redArray.count == 0, greenArray.count > 0
        {
    let index = Int(arc4random_uniform(UInt32(greenArray.count)))
    label.text = greenArray[index]
    redArray.remove(at: index)
    label.name = "green"
    (self.addChild(label))
        }
    else if redArray.count == 0, greenArray.count == 0, yellowArray.count == 0
        {showAlert(withTitle: "Congratulations!", message: "You've sorted all the invites. Press 'Finish' in the lower right corner of the screen.")
        }
    }

    //add the label to the scene
    label.addChild(SKSpriteNode(imageNamed:"blank2"))
}

0 个答案:

没有答案