Error VB NET - Simple game with shots

3

Hello, someone can help me solve the problem: When I shoot past 2 seconds nothing happens ... but when I shoot fast several bullets are paralyzed and react after: (

When firing several missiles they stop or hang only the missiles, this is the code that shows the missiles:

 Public Sub DispararNave(ByVal X As Integer, ByVal Y As Integer)
    Dim Bala As New PictureBox

    Me.Controls.Add(Bala)
    Bala.Width = 30
    Bala.Height = 10
    Bala.BorderStyle = BorderStyle.None
    Bala.BackColor = Color.Transparent
    Bala.BackgroundImage = My.Resources.NAVE_MALA
    Bala.BackgroundImageLayout = ImageLayout.Stretch
    Bala.Top = Y
    Bala.Left = X
    Bala.BringToFront()

    Dim i As Integer
    For i = 0 To 100
        Application.DoEvents()
        Bala.Location = New Point(Bala.Location.X, Bala.Location.Y - 20)
        Threading.Thread.Sleep(1)
    Next


End Sub

If someone could help me find the solution to this problem please:)

    
asked by Javier 02.06.2016 в 17:48
source

1 answer

1

I would put it in a different way: that the shoot method simply creates the bullet and there is a timer that executes a method that is responsible for changing the position of all the elements on the screen and redrawing it.

Look at this example:

Dim ReadOnly _bullets As New List(Of PictureBox)
Dim ReadOnly _screenUpdater As New Timer()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _screenUpdater.Interval = 20
    AddHandler _screenUpdater.Tick, AddressOf UpdateScreen
    _screenUpdater.Start()
End Sub

Private Sub UpdateScreen(sender As Object, e As EventArgs)
    If _bullets.Count = 0 Then Return

    For i As Integer = _bullets.Count - 1 To 0 Step -1
        Dim bullet = _bullets(i)
        If bullet.Location.Y < 20 Then
            Controls.Remove(bullet)
            _bullets.RemoveAt(i)
            bullet.Dispose()
        Else 
            bullet.Location = new Point(bullet.Location.X, bullet.Location.Y - 10)
        End If
    Next
    Application.DoEvents()
End Sub

Public Sub DispararNave(x As Integer, y As Integer)
    Dim bala As New PictureBox
    bala.Width = 30
    bala.Height = 10
    bala.BorderStyle = BorderStyle.None
    bala.BackColor = Color.Transparent
    bala.BackgroundImage = My.Resources.NAVE_MALA
    bala.BackgroundImageLayout = ImageLayout.Stretch
    bala.Top = Y
    bala.Left = X
    Controls.Add(bala)
    bala.BringToFront()
    _bullets.Add(bala)
End Sub

In the _bullets list I have all the bullets that are currently active.

The _screenUpdater timer is responsible for firing the UpdateScreen method that is responsible for updating the screen.

The ShootNave method simply creates the new PictureBox for the bullet in its initial position and adds it to the _bullets collection.

The UpdateScreen method is in charge of updating the position of all the bullets and refreshing the screen. It also removes the PictureBox from the controls of the form and from the _bullets list when it reaches a certain position.

    
answered by 02.06.2016 в 21:21