Win Coder
Win Coder

Reputation: 6756

building smooth animation in C#

First here's the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winform_project
{
  public partial class Form1 : Form
  {
    void movement_correct(PictureBox pic, int i)
    {
        System.Threading.Thread.Sleep(25);
        pic.Top += (i - 1);
        pic.Left += (i + 3);
    }

    void movement_up(PictureBox pic, int i)
    {
        System.Threading.Thread.Sleep(25);
        pic.Top -= (i - 1);
        pic.Left -= (i + 3);
    }
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            movement_correct(pictureBox1, i);
            movement_correct(pictureBox2, i);
            movement_correct(pictureBox3, i);
            movement_correct(pictureBox4, i);
            movement_correct(pictureBox5, i);
            movement_correct(pictureBox6, i);
            movement_correct(pictureBox7, i);
            movement_correct(pictureBox8, i);
            movement_correct(pictureBox9, i);
            movement_correct(pictureBox10, i);
        }

        for (int i = 0; i < 5; i++)
        {
            movement_up(pictureBox1, i);
            movement_up(pictureBox2, i);
            movement_up(pictureBox3, i);
            movement_up(pictureBox4, i);
            movement_up(pictureBox5, i);
            movement_up(pictureBox6, i);
            movement_up(pictureBox7, i);
            movement_up(pictureBox8, i);
            movement_up(pictureBox9, i);
            movement_up(pictureBox10, i);
        }

    }
 }
}

There are 10 dummy picture boxes. Whenever i click the button they start to move a bit(sort of like the enemies in space invaders).

I am trying to make a smooth animation in winforms. The problem is that the animation in not as smooth as i want it to be. In my code all the boxes aren't moving simultaneously, there is a little bit of noticeable gap in their movement. What i want is smooth movement.

I was thinking of multi threading it but then realized it would mean one thead for each objects. Is there any other way(Within winforms).

Thanks for the help.

Upvotes: 3

Views: 2785

Answers (3)

aamir
aamir

Reputation: 21

Getting the sleep() out of the methods and doing it once in loop iteration will reduce the delay from (more than 250ms) to (a little more than 25ms) between each box's movement_correct() and movement_up() call.

Upvotes: 1

Tyler Lee
Tyler Lee

Reputation: 2785

Calling this.SuspendLayout() and this.ResumeLayout() in your for loops will help keep all boxes moving at the same time. It may not get you the smoothness, but it will keep everything moving as one.

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75296

Using Thread.Sleep(n) for timing purposes is a guaranteed way of not getting smooth animation; this is because the value passed to Thread.Sleep is treated by the OS as a minimum sleep time, meaning that the actual duration of the sleep can be (and usually is) a bit longer. You should instead drive your animation using QueryPerformanceCounter.

Even with a proper high-resolution timing engine, you will still have "tearing" effects.

Upvotes: 1

Related Questions