Question 2 Pictureboxes disappear after 1 has been shot & shot continues upwards visible help!

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
2 Pictureboxes disappear after 1 has been shot & shot continues upwards visible help!

Public Class Codified
    Dim RRight As Boolean 'Controls the Renegade's movement to the right'
    Dim RLeft As Boolean 'Controls the Renegade's movement to the left'
    Dim RenegadeSpeed As Integer 'Controls the Renegade's speed'
    Dim Shotspeed As Integer 'Controls the Shot speed'
    Private Sub Codified_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadsettings() 'loadsettings'
    End Sub
    Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
        MoveRenegade() 'movement of the renegade'
        FireShot() 'controls the movement of the shot'
        CheckHit() 'checks weather the guards have been hit'
    End Sub
    Private Sub Codified_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'this code moves the Renegade to the right'
        If e.KeyValue = Keys.Right Then
            RRight = True
        End If
        If e.KeyValue = Keys.Left Then
            RLeft = True
            RRight = False
        End If
        ' this code fires the shot'
        If e.KeyValue = Keys.Space And Shot.Visible = False Then
            Shot.Top = Renegade.Top
            Shot.Left = Renegade.Left + (Renegade.Width / 2) - (Shot.Width / 2)
            Shot.Visible = True
        End If
    End Sub
    Private Sub Codified_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        'this code moves the Renegade left or right'
        If e.KeyValue = Keys.Right Then
            RRight = False
        End If
        If e.KeyValue = Keys.Left Then
            RLeft = False
        End If
    End Sub
    Private Sub MoveRenegade()
        'this code moves the Renegade'
        If RRight = True And Renegade.Left + Renegade.Width < Me.ClientRectangle.Width Then
            Renegade.Left += RenegadeSpeed
        End If
        If RLeft = True And Renegade.Left > Me.ClientRectangle.Left Then
            Renegade.Left -= RenegadeSpeed
        End If
    End Sub
    Private Sub FireShot()
        'this bit of code fires the shot'
        If Shot.Visible = True Then
            Shot.Top -= shotspeed
        End If
        If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then
            Shot.Visible = False
            Shot.Left = 5000
        End If
    End Sub
    Private Sub loadsettings()
        'this loads all the setting on the form'
        shotspeed = 6
        RenegadeSpeed = 6
        Shot.Visible = False
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Guard1.Left = Guard1.Left + 3 'moves guard1 to the left'
        Guard2.Left = Guard2.Left - 4 'moves guard1 to the right'


        If Guard1.Left > 1400 Then 'restart point of guard1'
            Guard1.Left = -100 'restart point of guard1'
        End If


        If Guard2.Left < -1400 Then 'end point of guard2'
            Guard2.Left = 500 'restart point of guard2'
        End If
    End Sub
    Private Sub CheckHit()
        'confirms that guard1 has been shot and restarts guard1'
        If Shot.Top < Guard1.Top + Guard1.Height And Shot.Left > Guard1.Left And Shot.Left < Guard1.Left + Guard1.Width Then
            Guard1.Visible = False
            LBLSCORE.Text = Val(LBLSCORE.Text) + 2
            Guard1.Left = -100
            Guard1.Visible = True
        End If
        'confirms that guard2 has been shot and restarts guard2'
        If Shot.Top < Guard2.Top + Guard2.Height And Shot.Left > Guard2.Left And Shot.Left < Guard2.Left + Guard2.Width Then
            Guard2.Visible = False
            LBLSCORE.Text = Val(LBLSCORE.Text) + 2
            Guard2.Left = -100
            Guard2.Visible = True
        End If
        If LBLSCORE.Text = 10 Then
            TimerMain.Stop() 'stops the main timer'
            Timer1.Stop() 'stops timer 1'
            Me.Hide() 'hides main form after the mission has being accomplished'
            MsgBox("MISSION ACCOMPLISHED") ' displays message after set value has been met.
        End If
    End Sub
 
You might want to try providing a full and clear description of the problem. Your title is good and you've posted well formatted and commented code. That's all good. Now provide a description of exactly what you're trying to achieve, what behaviour you expect to see and what behaviour you actually do see. You may think that we can work it all out from the title and the code but it will still take time that we shouldn't have to spend because you should explain all the details to us. It's for you to take that time, not us.
 
Wouldn't you just set the shot picturebox's visible property to False when you set the Guard that was shot's visible property to false?

Also you could save a lot of headache by using the .IntersectsWith() method for determining collision.

Instead of If shot.Top < Guard1.Top + Guard1.Height ...

You could use
If shot.bounds.IntersectsWith(Guard1.Bounds) Then
...
 
Last edited:
Wouldn't you just set the shot picturebox's visible property to False when you set the Guard that was shot's visible property to false?

Also you could save a lot of headache by using the .IntersectsWith() method for determining collision.

Instead of If shot.Top < Guard1.Top + Guard1.Height ...

You could use
If shot.bounds.IntersectsWith(Guard1.Bounds) Then
...

so wait, do I apply the intersect method to each guards (1,2,3,4,5,6,7,8) or is there a way of using one collision code to control all the guard.
 
Not sure but looking at the code above you have 2 guards that repeat when killed and in those two checks you comparing with the position and widths of the "guards" to see if the shot is within their bounds. So I just showed that you could instead just use .IntersectsWith and save yourself possible errors. Plus it makes it easier to read through.
 
Back
Top