首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Picturebox运动不同步

Picturebox运动不同步
EN

Stack Overflow用户
提问于 2021-10-24 15:29:55
回答 1查看 39关注 0票数 1

背景:我正试图在Visual中制作一个Flappy游戏来熟悉这种语言(我需要在接下来的两年里用它编写代码,我真的没有选择)。

问题是管道的持续移动。管道每隔1200毫秒产生一次。游戏检查每个控件标记的"pipe""score",并根据另一个间隔为60 on的定时器移动它。gameloop计时器代码:

代码语言:javascript
复制
    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

底部管道和得分区域都同步移动,但顶部似乎落后了一点(所有三个都应该一起移动),随着游戏的进行,看这里。得分区变得越来越糟糕,为了清晰起见,它的得分区域是粉红色的。

我不知道是什么导致了这个问题。我检查了gamelooppipegen定时器的运行时间,这两个定时器的代码在计时器间隔内执行得很好。我也尝试用case语句替换逻辑,但它仍然没有改变去同步化。我个人在网上找不到任何与此相关的东西。这是图片盒,VB的问题,还是我犯了一个愚蠢的错误(这是完全可能的,我对语言非常陌生)。

这是巴斯特箱的完整代码。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-25 10:26:20

我在代码中所犯的主要错误是在遍历数组时从数组中删除。相反,我列出了要删除的控件列表,并在此之后循环通过:

代码语言:javascript
复制
    ' 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次才能工作。

代码语言:javascript
复制
    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 Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69698215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档