我正在Visual中开发一个小型的超级马里奥游戏。我拍了两张照片,第一张是马里奥站立(png,不动),第二张是马里奥跑步(gif,3帧)。问题是,当我继续按“右”按钮时,gif中的3帧只处理一次,然后停止移动。
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发布于 2018-10-01 20:05:20
插入布尔检查。因此,如果Mario已经在运行,您就不会让它再次运行:)。
否则,您的PictureBox将继续只显示第一帧,因为您一直给它相同的动画一遍又一遍。
(我假设Level1是Form和KeyPreview = True)
正如Hans在注释中所指出的那样,将这些Image资源分配给类对象(在不再需要时可以使用.Dispose() )是个好主意。
UPDATE:基于注释,使用类对象进行相等比较,可以进一步简化动画状态检查。
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您可以使用您的FormClosing()或FormClosed()事件来处理图像。
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 Subhttps://stackoverflow.com/questions/52597334
复制相似问题