Reputation: 51
I'm developing a small Super Mario game in Visual Studio. I took 2 pictures of which the first one is Mario standing (png, not moving) and the second one is Mario running (gif, 3 frames). The problem is that, when I keep pressing on the "Right" button, the 3 frames inside the gif are processed only once then stop moving.
Private Sub Level1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Right
picBoxMario.Image = My.Resources.mario_running_right
End Select
End Sub
Private Sub Level1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
picBoxMario.Image = My.Resources.mario_standing_2
End Sub
Upvotes: 0
Views: 271
Reputation: 32223
Insert a Boolean check. So, if Mario is already running, you don't make it run again :).
Otherwise, your PictureBox
will keep on showing the first frame only, because you keep feeding it the same animation over and over.
(I assume Level1
is a Form
and KeyPreview = True
)
As Hans Passant noted in the comments, it's (more than) a good idea to assign those Image
Resources to class objects that you can then .Dispose()
when not needed anymore.
UPDATE: Based on the comments, an equality comparison using the class object, allows to further simplify the animation status check.
Private MarioRunning As Image = My.Resources.mario_running_right
Private MarioStanding As Image = My.Resources.mario_standing_2
Private Sub Level1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
picBoxMario.Image = MarioStanding
End Sub
Private Sub Level1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Right
If picBoxMario.Image.Equals(MarioRunning) Then Return
picBoxMario.Image = MarioRunning
End Select
End Sub
Private Sub Level1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
picBoxMario.Image = MarioStanding
End Sub
You could use the FormClosing()
or FormClosed()
events of your Form
to dispose of the Images.
Private Sub Level1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
If MarioRunning IsNot Nothing Then MarioRunning.Dispose()
If MarioStanding IsNot Nothing Then MarioStanding.Dispose()
End Sub
Upvotes: 1