c# - Dynamically Creating Image - Does not Exist in Current Context -
i trying create whackamole game in wpf in c#. bit of noob. in loop trying add number of "i" "image". following error:
"error name 'image1' not exist in current context"
same thing 'image2' , on. trying integrate images stackpanel.
thanks :)
public partial class mainwindow : window { image[] imagearray = new image[50]; public mainwindow() { moleini = molescore[1]; initializecomponent(); //string imagename = "image"; (int = 0; <= 8; i++) { image image = new image(); imagearray[i] = image; image.name = "image" + i.tostring(); } ////create images //for (int = 0; <= 8; i++) //{ // stackpanel1.children.add(createimage(i)); //} //dispacher mole 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 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 "1" image display if (num == 1) { imagesource moleimage = new bitmapimage(new uri(imgnamemole)); image1.source = moleimage; } //if random number not equal 1 if (num != 1) { imagesource hole = new bitmapimage(new uri(imghole)); image1.source = hole; } //if random number "2" image display if (num == 2) { imagesource moleimage = new bitmapimage(new uri(imgnamemole)); image2.source = moleimage; } }
use updated code:
private void dispatchertimer_tick(object sender, eventargs e) { //random number generator random rnd = new random(); int num = rnd.next(1, 9); //if random number "1" image display if (num == 1) { imagesource moleimage = new bitmapimage(new uri(imgnamemole)); imagearray[1].source = moleimage; } //if random number not equal 1 if (num != 1) { imagesource hole = new bitmapimage(new uri(imghole)); imagearray[1].source = hole; } //if random number "2" image display if (num == 2) { imagesource moleimage = new bitmapimage(new uri(imgnamemole)); imagearray[2].source = moleimage; } }
Comments
Post a Comment