Link to home
Start Free TrialLog in
Avatar of john-formby
john-formbyFlag for Ghana

asked on

Smooth animation

Hi,

What is the best way to make smooth animation in VB.NET?  I have a small game with a .JPG background and I have a few transparent GIFs which are animated using the timer control.  When the image moves though, it is very jumpy and the background is distorted.  It does not have to be perfect, but I would like it to be smoother than it is at the moment.

Thanks,

John.
Avatar of Lacutah
Lacutah
Flag of United States of America image

Under the initialization portion of the form (or control) where you are drawing the images, add the following line:

setstyle(ControlStyles.DubbleBuffer or ControlStyles.UserPaint or ControlStyles.AllPaintingInWmPaint, True)

This should help reduce the flickering.
Avatar of john-formby

ASKER

Hi,

Thanks for your very quick reply, I am making a small character move across the screen and it has 6 images to animate it.  The images are contained within an imagelist and the code that calls the images is:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        hsbSpeed_Change()

        steps += 1
        steps = steps Mod 5

        If imgPerson.Left = 735 Then
            direction = "left"
        ElseIf imgPerson.Left = 0 Then
            direction = "right"
        End If

        If direction = "left" Then
            imgPerson.Left = imgPerson.Left - 15
            imgPerson.Image = ImageList1.Images(steps + 6)
        ElseIf direction = "right" Then
            imgPerson.Left = imgPerson.Left + 15
            imgPerson.Image = ImageList1.Images(steps)
        End If

        CheckJump()
    End Sub

I am unsure where to add the piece of code you have just posted.

Please could you advise?

John
Actually, the code I advised is geared towards you using the form.onPaint event to create the graphics, not a graphics / image control, so it may not work as well.

Plase the snippit in the Public Sub New() event of the form (inside the windows form designer generated code.)
Not made any noticable difference i'm afraid, but thanks for trying :-)  Somebody mentioned something called bitblt.  Would this be any use in my situation?  I don't really know anything about it and how difficult it is to implement into the code.

John

It sounds like bitblt is an API function - something to avoid.

Why not simply handle the painting of the image(s) through the form's OnPaint call and it's associated graphics object?  Whenever timer1 fires, simply invalidate the form after changing a form-wide-variable called (arbitrairly) position.  Again, this is what my origional answer was prdicated on...

I imagine that the flickering you're seeing now is being caused by a dissacocioated control being relocated and redrawn on your form, then the form playing "catchup" and redrawing the region where the control used to be.  Using the OnPaint of the form to draw the images instead, you can redraw the entire form at once, and using double-buffering remove the flicker.
Avatar of natloz
natloz

bitblt I think is Direct X.
I tried the code you posted but it didn't make any difference :-(  I am probably not implementing it correctly :-)

I have posted my entire program for you to look at in a .zip file.  It is at:

http://www.3doubleu.co.uk/game.zip

If you could have a look at it I would really appreciate it

John
ASKER CERTIFIED SOLUTION
Avatar of Lacutah
Lacutah
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are a genius, that looks a 1000 times better than before :-)  Thank you so much.

John