背景:我正试图在Visual中制作一个Flappy游戏来熟悉这种语言(我需要在接下来的两年里用它编写代码,我真的没有选择)。
问题是管道的持续移动。管道每隔1200毫秒产生一次。游戏检查每个控件标记的"pipe"和"score",并根据另一个间隔为60 on的定时器移动它。gameloop计时器代码:
Private Sub gameloop_Tick(sender As Object, e As EventArgs) Handles gameloop.Tick
' continuous movements
player.Top += 10
For Each c As Control In Controls
If c.Tag = "pipe" Or c.Tag = "score" Then
If c.Left < -40 Then ' checking if it's off screen, then removing it if so
Controls.Remove(c)
Else ' if it's on screen, move it towards the player
c.Left -= 10
End If
End If
If c.Tag = "collider" Or c.Tag = "pipe" Then ' checking for collision
If player.Bounds.IntersectsWith(c.Bounds) Then
resetgame()
End If
End If
If c.Tag = "score" Then ' scoring if player goes between
If player.Bounds.IntersectsWith(c.Bounds) Then
Controls.Remove(c)
score += 1
scorelabel.Text = "Score: " & score
End If
End If
Next
End Sub底部管道和得分区域都同步移动,但顶部似乎落后了一点(所有三个都应该一起移动),随着游戏的进行,看这里。得分区变得越来越糟糕,为了清晰起见,它的得分区域是粉红色的。
我不知道是什么导致了这个问题。我检查了gameloop和pipegen定时器的运行时间,这两个定时器的代码在计时器间隔内执行得很好。我也尝试用case语句替换逻辑,但它仍然没有改变去同步化。我个人在网上找不到任何与此相关的东西。这是图片盒,VB的问题,还是我犯了一个愚蠢的错误(这是完全可能的,我对语言非常陌生)。
谢谢!
发布于 2021-10-25 10:26:20
我在代码中所犯的主要错误是在遍历数组时从数组中删除。相反,我列出了要删除的控件列表,并在此之后循环通过:
' game logic
Private Sub gameloop_Tick(sender As Object, e As EventArgs) Handles gameloop.Tick
' list of things to remove
Dim removethese As New List(Of Control)
' continuous movements
player.Top += 10
For Each c As Control In Controls
If c.Tag = "pipe" Or c.Tag = "score" Then
If c.Left < -40 Then ' checking if it's off screen, then removing it if so
removethese.Add(c)
Else ' if it's on screen, just move it towards the player
c.Left -= 10
End If
End If
If c.Tag = "collider" Or c.Tag = "pipe" Then ' checking for collision
If player.Bounds.IntersectsWith(c.Bounds) Then
resetgame()
End If
End If
If c.Tag = "score" Then ' scoring if player goes between
If player.Bounds.IntersectsWith(c.Bounds) Then
removethese.Add(c)
score += 1
scorelabel.Text = "Score: " & score
End If
End If
Next
For Each c As Control In removethese
Controls.Remove(c)
Next
End Sub现在所有管道都同步移动,所以我对destroy()函数做了同样的操作,我不再需要调用它3次才能工作。
Public Sub destroy()
' list of things to remove
Dim temp As New List(Of Control)
' collecting controls to remove
For Each c As Control In Controls
If c.Tag = "pipe" Or c.Tag = "score" Then
temp.Add(c)
End If
Next
' removing controls
For Each c As Control In temp
Controls.Remove(c)
Next
End Subhttps://stackoverflow.com/questions/69698215
复制相似问题