如何检查列表中的两个按钮是否具有相同的不透明度?
How to check if two button in a list have the same opacity?
我的申请有问题。我正在创建一个“简单”的手机游戏,其中有一个不透明度为 0.4 的按钮列表,我在列表中随机选择一个按钮并将其不透明度设置为 1。我的意思是生成两个按钮,所以现在我只是在开始时调用了该函数两次,但问题是当两个按钮相同时它只生成一个。我希望你能理解(我是法国人)。
''' void ChooseFirstRandomButton()
{
var rand = new Random();
var buttonList = new List<Button> {
one, two, three, four, five, six, seven, eight, nine,
ten, eleven, twelve, thirteen, fourteen, fifteen,
sixteen, seventeen, eighteen, nineteen, twenty,
twentyone, twentytwo, twentythree, twentyfour
};
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}
'''
“一”、“二”……是我按钮的名称。
感谢您的回答。
根据您提供的视频和您的描述“按钮彼此相遇”,您只需要改进您的算法:
- 在函数之外的某处创建一个全局变量:
int opacity_1_index = -1;
- 选择随机索引后,检查它是否不等于记住的那个:
int index = rand.Next(buttonList.Count);
if (index == opacity_1_index)
{
// exit function and call it again
}
- 否则请记住此索引并执行其余操作:
else
{
opacity_1_index = index; // add this line
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}
使用循环继续选择号码,直到您得到一个尚未被选择的号码
// initialize your list of buttons and random seed
// before the loop
bool loop = true;
while (loop)
{
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
// if it's not set, set it and exit loop
if (randomButton.Opacity != 1)
{
randomButton.Opacity = 1;
loop = false;
}
}
我的申请有问题。我正在创建一个“简单”的手机游戏,其中有一个不透明度为 0.4 的按钮列表,我在列表中随机选择一个按钮并将其不透明度设置为 1。我的意思是生成两个按钮,所以现在我只是在开始时调用了该函数两次,但问题是当两个按钮相同时它只生成一个。我希望你能理解(我是法国人)。
''' void ChooseFirstRandomButton()
{
var rand = new Random();
var buttonList = new List<Button> {
one, two, three, four, five, six, seven, eight, nine,
ten, eleven, twelve, thirteen, fourteen, fifteen,
sixteen, seventeen, eighteen, nineteen, twenty,
twentyone, twentytwo, twentythree, twentyfour
};
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}
'''
“一”、“二”……是我按钮的名称。
感谢您的回答。
根据您提供的视频和您的描述“按钮彼此相遇”,您只需要改进您的算法:
- 在函数之外的某处创建一个全局变量:
int opacity_1_index = -1;
- 选择随机索引后,检查它是否不等于记住的那个:
int index = rand.Next(buttonList.Count);
if (index == opacity_1_index)
{
// exit function and call it again
}
- 否则请记住此索引并执行其余操作:
else
{
opacity_1_index = index; // add this line
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}
使用循环继续选择号码,直到您得到一个尚未被选择的号码
// initialize your list of buttons and random seed
// before the loop
bool loop = true;
while (loop)
{
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
// if it's not set, set it and exit loop
if (randomButton.Opacity != 1)
{
randomButton.Opacity = 1;
loop = false;
}
}