0

I am trying to make a game where an image appears, and if it is not clicked the image should disappear. I need help giving my array a value of three, then subtract it in another method.

Code:

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

Thanks for the help!

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

リスト内のすべての要素に特定の値を与えたい場合は、foreach ループを使用します。この場合、次のようになります。

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

これにより、リストの各要素がループされ、3 に設定されます。

編集:配列を使用している場合は、次のようにします。

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