gpt4 book ai didi

arrays - 将值附加到数组并非不可能,如何将值添加到按钮/数组/图像?

转载 作者:行者123 更新时间:2023-11-30 10:02:20 25 4
gpt4 key购买 nike

我刚刚开始编码,所以我只是不知道大多数事情。我想做一个简单的游戏。我将简短地解释一下:当用户按下“单击开始”游戏按钮时,牌组中的 52 张牌中的 7 张将被揭开。我通过创建几个包含卡片背面图像的按钮来做到这一点。所以当用户点击按钮时,将会用一张随机卡牌打开 7 张卡牌。

首先,我创建了一个数组来将所有照片名称添加到 View Controller 中,如下所示:

var MasterAllCards = ["2_of_clubs", "2_of_spades", "2_of_diamonds", "2_of_hearts", "3_of_clubs", "3_of_spades", "3_of_diamonds", "3_of_hearts", "4_of_clubs", "4_of_spades", "4_of_diamonds", "4_of_hearts", "5_of_clubs", "5_of_spades", "5_of_diamonds", "5_of_hearts", "6_of_clubs", "6_of_spades", "6_of_diamonds", "6_of_hearts", "7_of_clubs", "7_of_spades","7_of_diamonds","7_of_hearts", "8_of_clubs", "8_of_spades", "8_of_diamonds", "8_of_hearts", "9_of_clubs", "9_of_spades", "9_of_diamonds", "9_of_hearts", "10_of_clubs", "10_of_spades", "10_of_diamonds", "10_of_hearts", "jack_of_clubs", "jack_of_spades", "jack_of_diamonds", "jack_of_hearts", "queen_of_clubs", "queen_of_spades", "queen_of_diamonds", "queen_of_hearts", "king_of_clubs", "king_of_spades", "king_of_diamonds", "king_of_hearts", "ace_of_clubs", "ace_of_spades", "ace_of_diamonds", "ace_of_hearts"]

然后,我从单击开始按钮创建了一个到 View Controller 的 IBAction 导出,如下所示:

@IBAction func testt(sender: UIButton) {
TheGoButton.enabled = false
TheGoButton.hidden = true
SlotCard1.setImage(UIImage(named: randomcardpicker("Cardslot1")), forState: UIControlState.Normal)
Playablecard1.setImage(UIImage(named: randomcardpicker("Playablecard1")), forState: UIControlState.Normal)
Playablecard2.setImage(UIImage(named: randomcardpicker("Playablecard2")), forState: UIControlState.Normal)
Playablecard3.setImage(UIImage(named: randomcardpicker("Playablecard3")), forState: UIControlState.Normal)
Playablecard4.setImage(UIImage(named: randomcardpicker("Playablecard4")), forState: UIControlState.Normal)
Playablecard5.setImage(UIImage(named: randomcardpicker("Playablecard5")), forState: UIControlState.Normal)
Playablecard6.setImage(UIImage(named: randomcardpicker("Playablecard6")), forState: UIControlState.Normal)
Playablecard7.setImage(UIImage(named: randomcardpicker("Playablecard7")), forState: UIControlState.Normal)
//sender.setImage(UIImage(named: randomcardpicker("Cardslot1")), forState: UIControlState.Normal)
}

我的函数“randomcardpicker”如下:

func randomcardpicker (WhatCarduPlay: String) -> String {
let randomIndex = Int(arc4random_uniform(UInt32(MasterAllCards.count)))
let RandomCard = AllCards[randomIndex]
let RandomValueOfTheCard = AllValues[randomIndex]
MasterAllCards.removeAtIndex(randomIndex)
AllValues.removeAtIndex(randomIndex)
StoreHereAllValues.append(RandomValueOfTheCard)
StoreHereAllCards.append(RandomCard)

return RandomCard
}
正如你所看到的,我添加了很多随机的东西,因为我不知道如何将附加值添加到数组中,例如 Int。现在按钮将正确更改为图像,但此按钮/图像需要保存一个值,我稍后可以使用该值来比较 2 个值。我读过一些有关枚举的内容,但我无法弄清楚这是否适用于我的代码。

所以我的问题是:如何向此按钮添加值,以便我将来可以用它与其他按钮进行比较?

当我写这篇文章时,我想让图像的值成为这个按钮的标题,这样我就可以轻松地读出该值。如果我错了请纠正我!

最佳答案

一种简单的方法可能是在每个 UIButton 上设置 tag 属性来确定您正在处理哪一个。

关于arrays - 将值附加到数组并非不可能,如何将值添加到按钮/数组/图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com