(Note: I know there’s a related Q+A, but it relates to the timer ending, whereas mine is to do with ticks)
This code is used to animate a rectangle moving between 2 points. At the moment, the first movement is perfect, but each time after that the animation becomes significantly faster and faster each time it is called.
I’m thinking it’s due to a misunderstanding on my part on how to call a rectangle movement on each tick.
Any help would be greatly appreciated!
This section starts the timer:
if (floorNo == 2)
{
timerDown.Tick += new EventHandler(timer_LiftDown);
timerDown.Interval = 10;
timerDown.Enabled = true;
timerDown.Start();
}
This code is called on each tick (right?):
void timer_LiftDown(object sender, EventArgs e)
{
rectangle1.Location = new Point(192, rectangle1.Location.Y + 2);
if (rectangle1.Location.Y >= 196)
{
timerDown.Stop();
}
}
I’ve cut out some unrelated code (e.g updating textboxes with current state, changing floorNo variable etc).
Thank you.
Instead of subscribing and unsubscribing to the event each time, I would suggest setting up the timer once, and then just calling start/stop on it.
That way, you only have to initialize the timer and set the interval once, and can start/stop at will.