MysteryGuest
Registered Member
- Mar 7, 2013
- 64
- 18
Hello,
For those that having issue's with moving images smooth, here you have a solution too fix those problems! let's start right away!
Start with adding a timer or a thread i will keep them separate; let's start with the timer!
TIMER
So now with threading; it's basically the same.
almost forgot, add this to constructor. this count for both timer and threading!
For those that having issue's with moving images smooth, here you have a solution too fix those problems! let's start right away!
Start with adding a timer or a thread i will keep them separate; let's start with the timer!
TIMER
Code:
Ok, so let's start with adding a timer. tmrMain;
Properties, INTERVAL = 1;
Start with declaring the image
and the nessary USING
using System.Drawing;
#region declare
int _carsXValue;
private const int CarSpeed = 2;
readonly Image _fastCar=Image.FromFile("fastcar.gif"); //Image has to be in debug folder or release;
Now for the timer!
private void tmrMain_Tick(object sender, EventArgs e)
{
if (_carsXValue > 0)
_carsXValue -= CarSpeed;
else
{
_carsXValue = 672;
}
Invalidate();
}
and lets make Pain events.
void OnPaint(Object sender, PaintEventArgs e)
{
var g = e.Graphics;
DrawCar(g);
}
void DrawCar(Graphics g)
{
g.DrawImage(_fastCar,_carsXValue,100);
}
#endregion
So now with threading; it's basically the same.
Code:
using System.Threading;
private bool StopThreadCar = true;
private void startCar()
{
var thread = new Thread(Go);
thread.Start();
}
void Go()
{
while (StopThreadCar == false)
{
if (_carsXValue > 0)
_carsXValue -= CarSpeed;
else
{
_carsXValue = 672;
}
Invalidate();
Thread.Sleep(1);
}
}
void OnPaint(Object sender, PaintEventArgs e)
{
var g = e.Graphics;
DrawCar(g);
}
void DrawCar(Graphics g)
{
g.DrawImage(_fastCar,_carsXValue,100);
}
now to let the car move make a button and call it by using
StopThreadCar = false;
StartCar();
almost forgot, add this to constructor. this count for both timer and threading!
Code:
Paint += new PaintEventHandler(OnPaint); // make a paint event
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
Last edited: