您好我的VB.NET动画代码需要帮助。 我最近买了一本Johnathan S. Harbour的编程书,但在他关于2D动画的章节中,我在最后的练习中遇到了一个非常恼人的错误。在构建了一个游戏和精灵类之后,我不得不制作一个动画龙,但它似乎导致我所获得的错误会引用游戏和精灵类,但是当我将它们应用到代码中时它没有任何区别。我遇到麻烦的两个功能被称为游戏和精灵,这是我的代码感谢您的帮助。
Public Class Form1
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
shutdown()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
game_keypressed(e.KeyCode)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Main()
End Sub
End Class
Public Module module1
Private p_gameover As Boolean = False
Private p_starttime As Integer = 0
Private p_currenttime As Integer = 0
Public game As Game
Public dragonimage As Bitmap
Public dragonsprite As sprite
Public grass As Bitmap
Public framecount As Integer = 0
Public frametimer As Integer = 0
Public framerate As Single = 0
Public direction As Integer = 2
Public velocity As PointF
Public Sub main()
game = New game(Form1, 800, 600)
REM load and intilize game assets
game_init()
While Not p_gameover
REM update timer
p_currenttime = My.Computer.Clock.TickCount()
REM let gameplay code update
game_update(p_currenttime - p_starttime)
REM refresh at 60 fps
If p_currenttime > p_starttime + 16 Then
REM update timing
p_starttime = p_currenttime
REM let gameplay code draw
game_draw()
REM give the form some cycles
Application.DoEvents()
game.update()
End If
framecount += 1
If p_currenttime > frametimer + 1000 Then
frametimer = p_currenttime
framerate = framecount
framecount = 0
End If
End While
REM free memory and shut down
game_end()
End Sub
Public Sub shutdown()
p_gameover = True
End Sub
Public Sub game_end()
dragonimage = Nothing
dragonsprite = Nothing
grass = Nothing
End Sub
Public Function game_init() As Boolean
Form1.Text = "sprite drawing demo"
grass = game.loadbitmap("grass.bmp")
dragonimage = game.loadbitmap("dragon.png")
dragonsprite = New sprite(game)
dragonsprite.image = dragonimage
dragonsprite.width = 256
dragonsprite.height = 256
dragonsprite.collumns = 8
dragonsprite.totalframes = 64
dragonsprite.animationrate = 20
dragonsprite.x = 250
dragonsprite.y = 150
Return True
End Function
REM not currently used
Public Sub game_update(ByVal time As Integer)
End Sub
Public Sub game_draw()
REM draw background
game.drawbitmap(grass, 0, 0, 800, 600)
REM move the dragon sprite
Select Case direction
Case 0 : velocity = New Point(0, -1)
Case 2 : velocity = New Point(1, 0)
Case 4 : velocity = New Point(0, 1)
Case 6 : velocity = New Point(-1, 0)
End Select
dragonsprite.x += velocity.X
dragonsprite.y += velocity.Y
REM animete and draw dragon sprite
dragonsprite.animate(direction * 8 + 1, direction * 8 + 7)
dragonsprite.draw()
End Sub
Public Sub game_keypressed(ByVal key As System.Windows.Forms.Keys)
Select Case key
Case Keys.Escape : shutdown()
Case Keys.Up : direction = 0
Case Keys.Right : direction = 2
Case Keys.Down : direction = 4
Case Keys.Left : direction = 6
End Select
End Sub
End Module