none
Need help with forcing pictureboxes to overlap RRS feed

  • Question

  • I'm just starting coding on visual basic in my cp science class and am attempting to create a project where the player can aim crosshairs with his mouse pointer and shoot aliens before they catch him/her.

    Heres code I have so that crosshairs follow your mouse. (which works)

      Private Sub Form1_MouseMove(ByVal sender As Object,
                                    ByVal e As System.Windows.Forms.MouseEventArgs) _
                                    Handles Me.MouseMove
         

        Private AimLoc As Point

    Cursor.Hide()
            AimLoc = New Point(Cursor.Position.X, Cursor.Position.Y)

    Crosshairs.Location = New Point(AimLoc.X - 300, AimLoc.Y - 200) 'Have-300 and -200 to compensate for difference between cursors coordinates and where it actually appears to be located.

        End Sub

    I also have the alien that always moves toward your character.

    Private Sub TimerEnemy_Tick(sender As Object, e As EventArgs) Handles TimerEnemy.Tick
            If PictureBox1.Top > PictureBox2.Top Then
                PictureBox1.Top = PictureBox1.Top - 1
            End If
            If PictureBox1.Top < PictureBox2.Top Then
                PictureBox1.Top = PictureBox1.Top + 1
            End If
            If PictureBox1.Left > PictureBox2.Left Then
                PictureBox1.Left = PictureBox1.Left - 1
            End If
            If PictureBox1.Left < PictureBox2.Left Then
                PictureBox1.Left = PictureBox1.Left + 1
            End If      
        End Sub

    The problem I am encountering is the crosshairs will move anywhere on the screen EXCEPT on top of the alien. Which obviously is a problem as it is impossible to shoot the alien. I am assuming it is a problem with the properties of pictureboxes, which don't want to be stacked on eachother. But is there a way to fix this?

    Hope that made sense, wanted to attach image but account wasn't verified.

    Tuesday, April 24, 2018 3:49 PM

Answers

  • My advice is to dump the pictureboxes now that you see some of the limitations of using pictureboxes for this type of thing.

    Start drawing everything on the form or one picturebox. Draw rectangles where you currently have a picturebox or whatever. Draw the entire scene all at once in the paint event for the control.


    PS When the mouse pointer is over the pictureboxes you have now, the picturebox mouse move event is being called by the system, not the form1MouseMove event.
    • Edited by tommytwotrain Tuesday, April 24, 2018 4:17 PM
    • Marked as answer by CoderNoobie Tuesday, April 24, 2018 4:30 PM
    Tuesday, April 24, 2018 4:15 PM