mardi 24 février 2015

How do I remove flickering from a moving object?

I've been at this for a few weeks... boiled down to its simplest component, I want to move one single circle from the left side of the screen to the right side in a SMOOTH motion, without any flickering.


Here's my code: I instantiated a timer (timer1) and enabled in in the form's properties window and have tried values from 1-100.



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

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private int _y;
private int _x;

public Form1()
{
InitializeComponent();
_x = 0;
_y = 200;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Green, _x, _y, 30, 30);
}

private void timer1_Tick(object sender, EventArgs e)
{
_x += 1;
Invalidate(); //ie: redraw
}

}
}

Aucun commentaire:

Enregistrer un commentaire