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.
LVL 14
john-formbyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LacutahCommented:
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.
0
john-formbyAuthor Commented:
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
0
LacutahCommented:
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.)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

john-formbyAuthor Commented:
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

0
LacutahCommented:
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.
0
natlozCommented:
bitblt I think is Direct X.
0
john-formbyAuthor Commented:
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
0
LacutahCommented:
http://www.eprojects247.com/updated.zip

Added two lines to your Timer1 Event which helped a tiney bit (imgPerson.Visible = False and imgPerson.Visible = True), also created a demo form showing how to draw the graphics dirrectly to the form and how that DOES improve the preformance rather than using image controls.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
john-formbyAuthor Commented:
You are a genius, that looks a 1000 times better than before :-)  Thank you so much.

John
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.