I am trying to create a WhackaMole game in WPF in C#. I am a bit of a noob. In the for loop I am trying to add the number of "i" to the "Image". I get the following error:
"Error The name 'Image1' does not exist in the current context"
same thing for 'Image2' and so on. I am trying to integrate the images into a StackPanel.
Thanks for the help :)
public partial class MainWindow : Window
{
Image[] ImageArray = new Image[50];
public MainWindow()
{
Moleini = MoleScore[1];
InitializeComponent();
//string ImageName = "Image";
for (int i = 0; i <= 8; i++)
{
Image Image = new Image();
ImageArray[i] = Image;
Image.Name = "Image" + i.ToString();
}
////Create Images
//for (int i = 0; i <= 8; i++)
//{
// StackPanel1.Children.Add(CreateImage(i));
//}
//Dispacher for Mole to Appear
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();
//Dispacher for Full Game Time
System.Windows.Threading.DispatcherTimer endGame = new System.Windows.Threading.DispatcherTimer();
endGame.Tick += new EventHandler(endGame_Tick);
endGame.Interval = TimeSpan.FromSeconds(5);
endGame.Start();
}
////Create Image
//public Image CreateImage(int i)
//{
//}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//Random Number Generator
Random rnd = new Random();
int num = rnd.Next(1, 9);
//If Random Number is "1" Then Image will display
if (num == 1)
{
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Image1.Source = MoleImage;
}
//If Random Number does not equal 1
if (num != 1)
{
ImageSource hole = new BitmapImage(new Uri(ImgHole));
Image1.Source = hole;
}
//If Random Number is "2" Then Image will display
if (num == 2)
{
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Image2.Source = MoleImage;
}
}