0

我正在尝试制作一个出现图像的游戏,如果没有点击它,图像应该会消失。我需要帮助给我的数组一个值三,然后用另一种方法减去它。

代码:

NameCount = -1;
NameCount++;

        Grid.SetColumn(mole, ranCol);
        Grid.SetRow(mole, ranRow);
        grid_Main.Children.Add(mole);

        for (int i = 0; i < NumofImages; i++)
        {
                //Where I must give a value to the array of the array to 3 for every image that appears.
        }

 //Where I am trying to make the image disappear after 3 seconds.
        private void deleteMole()
        {

            NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
            NumberofImages = Convert.ToInt32(NumofImages);

            for (int j = 0; j < NumofImages; j++)
            {

                CounterArray[j]--;

                if (CounterArray[j] == 0)
                {
//Not Sure How to delete image

谢谢您的帮助!

4

2 回答 2

1

您可以跟踪另一个数组中的图像。

将图像添加到视图后,您还应该将其添加到数组中:

images[j] = mole;

然后后来:

if (CounterArray[j] == 0)
{
    grid_Main.Children.Remove(images[j]);
}

但是使用静态数组和分离数据并不是一个好主意。

如果可以的话,最好将所有元数据和图像聚合在同一个结构中:

class Mole
{
    public int Counter { get; set; }
    public Control Image { get; set; }
}

并在单个List<Mole>中管理它们;添加和删​​除它们会更简单。

这是一些说明这个想法的代码(不会编译):

class Mole
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Counter { get; set; }
    public Control Image { get; set; }
    public bool IsNew { get; set; }
}

class Test
{   
    IList<Mole> moles = new List<Mole>();

    private static void AddSomeMoles()
    {
        moles.Add(new Mole{ X = rand.Next(100), Y = rand.Next(100), Counter = 3, Image = new PictureBox(), IsNew = true });
    }

    private static void DisplayMoles()
    {
        foreach (Mole mole in moles)
        {
            if (mole.IsNew)
            {
                grid_Main.Children.Add(mole.Image);
                mole.IsNew = false;
            }
        }
    }

    private static void CleanupMoles()
    {
        foreach (Mole mole in moles)
        {
            mole.Counter -= 1;

            if (mole.Counter <= 0)
            {
                grid_Main.Children.Remove(mole.Image);
                moles.Remove(mole);
            }
        }
    }

    static void Main()
    {   
        while (true)
        {
            AddSomeMoles();

            DisplayMoles();

            Thread.Sleep(1000);

            CleanupMoles();
        }
    }
}
于 2013-06-06T17:47:53.153 回答
1

如果要给 List 中的每个元素一个特定的值,请使用 foreach 循环。在这种情况下,它看起来像:

foreach(int currentElement in CounterArray)
{
    currentElement = 3;
}

这将遍历 List 的每个元素并将其设置为 3。

编辑:如果您使用的是数组,您将执行以下操作:

for (int i = 0; i < CounterArray.Length; i++)
{
    CounterArray[i] = 3;
}
于 2013-06-06T19:19:58.077 回答