Having trouble iterating through a stack for a "Simon" game. I created a random string generator to populate a stack to iterate through to trigger timers, and I can only get it to iterate the same index over and over. How do I get it to return to timer() and go to the next item in the stack (mySimon)? (without using Pop(), of course.) Cause right now, it just loops infinitely as you can surely see. To save space, I omitted the cases for each color and only included green because they are all identical. "green" refers to the actual button that flashes. timer() is actually called from a playback button each time, as well. Feel free to alter my code and re-post it with any suggestions. Thanks!
I should add that the interval of 1 millisecond is fine, because that's just the tick that's getting incremented. The actual blink I'm getting to happen is sufficient, my trouble is with getting it to loop properly.
public Window1() { InitializeComponent(); } public void timer() { foreach (string i in mySimon) if (isRunning == false) { switch (i) { case "myGreen": { DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(1); timer.Tick += new EventHandler(greenCount); timer.Start(); isRunning = true; Thread.Sleep(100); break; } } } } public void greenCount(Object sender, EventArgs args) { counter++; simonText.Text = counter.ToString(); if (counter < 29) { green.Background = Brushes.LawnGreen; } if (counter > 29) { DispatcherTimer myTimer = (DispatcherTimer)sender; myTimer.Stop(); counter = 0; green.Background = Brushes.Green; isRunning = false; timer(); } }
timer.Interval = TimeSpan.FromMilliseconds(1);
This is insane. You think you can see one millisecond ? 30 milliseconds is WAY too short. And why have a timer if you're going to use a counter inside it ? Why not set the timer to 500 milliseconds and just swap every time ?
isRunning
timer.Start()
greenCount()
timer()
DispatcherTimer
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)