Andrew
Andrew

Reputation: 545

Give an Array a Value

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!

Upvotes: 0

Views: 244

Answers (2)

Pragmateek
Pragmateek

Reputation: 13354

You could keep track of the images in another array.

After you add the image to the view you should also add it to the array:

images[j] = mole;

Then later:

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

But using static arrays and separating data is not a good idea.

If you can you should better aggregate all the metadata and the image together in the same structure:

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

and manage them in a single List<Mole>; adding and removing them will be simpler.

Here is some code that illustrates the idea (won't compile):

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();
        }
    }
}

Upvotes: 1

Sean Cogan
Sean Cogan

Reputation: 2586

If you want to give every element in a List a certain value, use a foreach loop. In this case, it would look like:

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

This will loop through each element of the List and set it to 3.

EDIT: If you're using an array, which you are, you would do the following:

for (int i = 0; i < CounterArray.Length; i++)
{
    CounterArray[i] = 3;
}

Upvotes: 1

Related Questions