samreay
New member
- Joined
- Jul 16, 2009
- Messages
- 1
- Programming Experience
- 1-3
Hey guys, Ive been trying to make basically a simple animation player inside a class so that I can use it in a game I have to make for a school IPT project.
The problem is hard to explain, but I will try.
When I first tested out some code I found, it was not in "class form", ie there were just loose variables as I was only animating a single .gif file. The code worked perfectly, and the animation was drawn frame by frame by the e.Graphics.DrawImage function.
Now that I have moved the code into a class (So I can run more than one animation at a time, though Im still trying to get only one to work), The e.Graphics.DrawImage function didnt work. It would draw the first image, but then (even though the function is called as shown by stepping through the lines of code), the picture doesn't update. I know the frame has changed as Ive kept track of the current frame in the debug window, but it still wont work.
I have had troubles like this with other GDI functions, where if the variable to draw is inside a class (etc) it simply wont work, it will only work if it is a loosely floating variable. (ie Sprite.Image doesnt work, If i just make an image it does). Any help would be appreciated.
With the code below, I have tried in the e.Graphics.DrawImage function submitting me.wth.animatedImage and wth.animatedImage
Public Class Form1
Dim wth As New vsprite
Public Class vsprite
Public animatedImage As New Bitmap("C:\Documents and Settings\Sam\Desktop\explosion.gif")
Public currentlyAnimating As Boolean = False
Public frameNum As Integer
Public NumberOfFrames As Integer
Public Sub AnimateImage()
If Not currentlyAnimating Then
Dim FrameDimensions As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(animatedImage.FrameDimensionsList(0))
NumberOfFrames = animatedImage.GetFrameCount(FrameDimensions)
'Begin the animation only once.
ImageAnimator.Animate(animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
currentlyAnimating = True
frameNum = 1
End If
End Sub
Public Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
'Force a call to the Paint event handler.
Form1.Invalidate()
Me.frameNum += 1
Debug.Write(Me.frameNum & " ")
If Me.frameNum = Me.NumberOfFrames Then
ImageAnimator.StopAnimate(Me.animatedImage, AddressOf Me.OnFrameChanged)
'currentlyAnimating = False
Me.frameNum = 1
End If
End Sub
End Class
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'Begin the animation.
wth.AnimateImage()
'Get the next frame ready for rendering.
ImageAnimator.UpdateFrames()
'Draw the next frame in the animation.
e.Graphics.DrawImage(Me.wth.animatedImage, New Point(0, 0))
End Sub
End Class
The problem is hard to explain, but I will try.
When I first tested out some code I found, it was not in "class form", ie there were just loose variables as I was only animating a single .gif file. The code worked perfectly, and the animation was drawn frame by frame by the e.Graphics.DrawImage function.
Now that I have moved the code into a class (So I can run more than one animation at a time, though Im still trying to get only one to work), The e.Graphics.DrawImage function didnt work. It would draw the first image, but then (even though the function is called as shown by stepping through the lines of code), the picture doesn't update. I know the frame has changed as Ive kept track of the current frame in the debug window, but it still wont work.
I have had troubles like this with other GDI functions, where if the variable to draw is inside a class (etc) it simply wont work, it will only work if it is a loosely floating variable. (ie Sprite.Image doesnt work, If i just make an image it does). Any help would be appreciated.
With the code below, I have tried in the e.Graphics.DrawImage function submitting me.wth.animatedImage and wth.animatedImage
Public Class Form1
Dim wth As New vsprite
Public Class vsprite
Public animatedImage As New Bitmap("C:\Documents and Settings\Sam\Desktop\explosion.gif")
Public currentlyAnimating As Boolean = False
Public frameNum As Integer
Public NumberOfFrames As Integer
Public Sub AnimateImage()
If Not currentlyAnimating Then
Dim FrameDimensions As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(animatedImage.FrameDimensionsList(0))
NumberOfFrames = animatedImage.GetFrameCount(FrameDimensions)
'Begin the animation only once.
ImageAnimator.Animate(animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
currentlyAnimating = True
frameNum = 1
End If
End Sub
Public Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
'Force a call to the Paint event handler.
Form1.Invalidate()
Me.frameNum += 1
Debug.Write(Me.frameNum & " ")
If Me.frameNum = Me.NumberOfFrames Then
ImageAnimator.StopAnimate(Me.animatedImage, AddressOf Me.OnFrameChanged)
'currentlyAnimating = False
Me.frameNum = 1
End If
End Sub
End Class
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'Begin the animation.
wth.AnimateImage()
'Get the next frame ready for rendering.
ImageAnimator.UpdateFrames()
'Draw the next frame in the animation.
e.Graphics.DrawImage(Me.wth.animatedImage, New Point(0, 0))
End Sub
End Class