Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net more performance for moving objects

Tags:

vb.net

I have the mission to make a small game for a school project. Pictures boxes, moved by a timer for walking enemies.If there are around 5 or 6 moving picture boxes at the form, my application get troubles and lags. After I kill some enemies (remove them from the Controls Collection of the Form/Panel) It come back smooth.

I think the loop of the enemy movement is too complicated but I don't know how to make that simpler.

Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
    For Each Enemy As Control In PanelBackground.Controls
        If Enemy.Name.Substring(0, 5) = "Enemy" Then
            _enemy.MoveEnemy(Enemy, 2)
        End If
    Next
End Sub

I also thought about Multithreading but not sure this would solve the problem and there is also the problem that I can't access the Controls of my mainform.

You see, I don't have much knowledge about vb.net

Any ideas how to fix that lag?

like image 463
René Stalder Avatar asked Feb 16 '26 07:02

René Stalder


1 Answers

Try this:

Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
    SuspendLayout()
    For Each Enemy As Control In PanelBackground.Controls
        If Enemy.Name.Substring(0, 5) = "Enemy" Then
            _enemy.MoveEnemy(Enemy, 2)
        End If
    Next
    ResumeLayout()
End Sub
like image 101
Joel Coehoorn Avatar answered Feb 18 '26 02:02

Joel Coehoorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!