vb.net more performance for moving objects
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_开发者_Go百科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?
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
VB.NET WinForm apps aren't the ideal setup to create games or moving objects. The Painting of the form uses to much performance as you have found out.
Try moving to a VB.NET WPF (Windows Presentation Framework) application it handles graphics a lot better.
Just a couple of suggestions, that may improve speed
a) Rather than going through through all the controls, how about storing them in an array / list b) old game trick is to draw the scene in memory and then copy it to screen .. so why not have a memory bitmap (or graphic) draw the "enemies" to this and then copy to entire bmp to screen once all is done
Do all the enemies need to move at the same time? Do some move faster?
if there are 6 enemies, you could move 1,3,5 on the first tick, then 2.4.6 on the next tick etc.
?
I've been using XNA for creating a simple game, it was really cool. Though VB.NET is not officially supported, you can make it work. It's optimized for scenery and large number of animated objects.
精彩评论