none
Set the range of where the sprites attack from...

    Question

  • Afternoon everybody, is there any examples people have used where they set the range of how far can an actual sprite (Tank) attack from in terms of grid reference for example one sprite can attack to the maximum of 5 squares or move to the maximum 5 squares and determine if a tank or building or turret is within that range?

    WRA

    Monday, April 3, 2017 11:13 AM

Answers

  • But one problem Tommy, it won't allow me to select from sprite to sprite such as select another sprite other than the one I just selected as seen with the screenshots. 

    How would I just using your example adjust it to select from sprite to sprite?


    WRA


    Well thats looking better Wali.

    I am not sure what problem you are having. I dont have the code.

    My example allows you to keep picking the target as I recall. If an invalid target is selected the example gives a message and awaits selection of a valid target.

    After the attack sprite is set the example only selects target unless you add code to reset. ie AttackPicturebox = nothing. Otherwise you can keep selecting targets at that stage.

    So whatever you use to determine what stage you are at, ie in my example if AttackPicturebox is not nothing then choose target, I use AttackPicturebox as my flag, just reset the flag to step 1 or whatever.

    You may need to add a cancel button. Or use rmb for cancel etc. Or if target not in range then reset target select or whatever.

    • Marked as answer by Waliur Rahman Saturday, April 15, 2017 3:02 PM
    Saturday, April 15, 2017 2:54 PM

All replies

  • Use pathagorean.

    This gives distance between two pictureboxes.

    Public Class Form2
        Private WithEvents pic1 As New PictureBox With {.Parent = Me, .Location = New Point(40, 40), .BackColor = Color.Red}
        Private WithEvents pic2 As New PictureBox With {.Parent = Me, .Location = New Point(170, 120), .BackColor = Color.Blue}
        Private label1 As New Label With {.Parent = Me, .Location = New Point(50, 5), .Font = New Font("tahoma", 12, FontStyle.Bold), .AutoSize = True}
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim angle, distance As Double
    
            'get the distance between upper left of pictureboxes
            MathAngleDistanceToPoint(pic1.Location, pic2.Location, angle, distance)
    
            label1.Text = angle.ToString("f1") & "   " & distance.ToString("f1")
    
        End Sub
    
        Private Sub MathAngleDistanceToPoint(ByVal ptFrom As PointF, ByVal ptTo As PointF, ByRef angle As Double, ByRef distance As Double)
            'units are pixels and degrees
            'angle 0 is x axis right and increasing clockwise
            Dim dx As Double = ptFrom.X - ptTo.X
            Dim dy As Double = ptFrom.Y - ptTo.Y
            angle = 180 + CSng(57.8 * Math.Atan2(dy, dx))
            If angle > 360 Then angle -= 360
            If angle < 0 Then angle += 360
            distance = Math.Sqrt((dx * dx) + (dy * dy))
        End Sub
    End Class

    Monday, April 3, 2017 2:09 PM
  • @Tommy

    damn gotta study how this actually works before I implement it into my own project like WOAH. Thanks for the example Tommy


    WRA

    Monday, April 3, 2017 2:17 PM
  • @Tommy 

    Thanks again for the example it has provided me insight on how I could set the range, is there a more simplified example which show the position of each tank like it would in a game for instance "battleships" which shows its position in (x, y)?

    I don't want it to be much complex just a style which shows how far a tank could attack from and determine if anything is in range and if they are able to attack it. 


    WRA

    Monday, April 3, 2017 5:27 PM
  • I don't want it to be much complex just a style which shows how far a tank could attack from and determine if anything is in range and if they are able to attack it.

    You need to define what you mean by 'in range'.  You mention, for instance, 5 squares.  That's obvious if the two positions are in the same row or the same column.  But how do you count '5' if the rows or columns are different?

    Range is usually defined as a distance, not as a number of squares.

    Monday, April 3, 2017 8:59 PM
  • @Tommy 

    Thanks again for the example it has provided me insight on how I could set the range, is there a more simplified example which show the position of each tank like it would in a game for instance "battleships" which shows its position in (x, y)?

    I don't want it to be much complex just a style which shows how far a tank could attack from and determine if anything is in range and if they are able to attack it. 


    WRA

    Wali,

    Whether you show units of rows and columns or pixels up and sideways makes no difference. Those are just units. You still have to compare one distance to another. There is no other magic way? In this case range is by definition the distance.

    The example is pretty easy really. You dont have to understand the math, just the function. Call the function with two points and get back the distance and angle. The function will work for any units. Pixels, feet, Rows, Horses, etc.

    When you say (x, y) of a battleship what are you talking about in your game? In your game the battleship x,y is the picturebox location. It just depends on what units you use, pixels or grid rows.  You can work with and show the data any way you want. Pixels, rows, horses just convert the units. You need to make functions to convert back and forth from pixels to rows and columns on your grid. You already do this to locate the pictureboxes.

    You have x / yourgridratio ie ratio = client.width(pixels) / columns = pixels / column to convert with.

    So you can see in the picture if you loop through the sprites and calc the distances you can determine in range or not (say the range is 6 in the image).

    Tuesday, April 4, 2017 12:28 PM
  • Ohhh thanks for clarifying that Acamar there goes me again assuming other people know what I am on about

    WRA

    Tuesday, April 4, 2017 1:12 PM
  • @Tommy

    This is advanced maths I am looking at here damn.

    In other words the use of the angle feature here is useful.

    I know I can use a feature to move the picturebox about. Would It automatically update the location of the new picturebox and its range?

    plus rather than using degress and the 1000s numbers is it possible to convert it to something simplistic as just 6 squares across or up etc.?


    WRA

    Tuesday, April 4, 2017 1:15 PM
  • Private Function GetGridRectFromColRow(thisColRow As Point) As Rectangle
            'make the rectangle for the grid square at col row
            GetGridRectFromColRow = New Rectangle(GridRect.X + (thisColRow.X * GridSize.Width),
                            GridRect.Y + (thisColRow.Y * GridSize.Height), GridSize.Width, GridSize.Height)
        End Function
    
        Private Function GetColRowFromPoint(thisPt As Point) As Point
            'convert pixel location to grid cols and rows
            GetColRowFromPoint = New Point(CInt(Math.Floor((thisPt.X - GridRect.X) / GridSize.Width)),
                                CInt(Math.Floor((thisPt.Y - GridRect.Y) / GridSize.Height)))
    
            If GetColRowFromPoint.X < 0 Then GetColRowFromPoint.X = 0
            If GetColRowFromPoint.X > GridColRows.X - 1 Then GetColRowFromPoint.X = GridColRows.X - 1
            If GetColRowFromPoint.Y < 0 Then GetColRowFromPoint.Y = 0
            If GetColRowFromPoint.Y > GridColRows.Y - 1 Then GetColRowFromPoint.Y = GridColRows.Y - 1
        End Function
    Quick question Tommy I know this isn't relevant to the thread but how can I adjust the size of each square in my grid for width and height, I want to make them bigger.

    WRA

    Tuesday, April 4, 2017 1:39 PM
  • @Tommy

    This is advanced maths I am looking at here damn.

    In other words the use of the angle feature here is useful.

    I know I can use a feature to move the picturebox about. Would It automatically update the location of the new picturebox and its range?

    plus rather than using degress and the 1000s numbers is it possible to convert it to something simplistic as just 6 squares across or up etc.?


    WRA

    What school level are you? I mean University Masters degree or what? Just helps us to know. And this is a beginning programing class? Perhaps this game is too advanced for your level? Or you will have to limit what you do exactly. Or not.

    Lucky for you its raining. Here is another example. The image is after clicking the blue pic.

    after clicking white pic:

    Public Class Form2
        Private WithEvents picRed As New PictureBox With {.Parent = Me}
        Private WithEvents picBlue As New PictureBox With {.Parent = Me}
        Private WithEvents picGreen As New PictureBox With {.Parent = Me}
        Private WithEvents picWhite As New PictureBox With {.Parent = Me}
        Private GridColsRows As New Point(8, 8)
        Private GridSquareSizePixels As New Size(50, 50)
        Private ScaleRatio As Double  'pixels per column
        Private PicRedCRGridPt As New Point(2, 3)  'pic location in grid (column,rows)
        Private PicBlueCRGridPt As New Point(5, 4)
        Private PicGreenCRGridPt As New Point(3, 6)
        Private PicWhiteCRGridPt As New Point(0, 6)
        Private SelectedPic As PictureBox
        Private TargetPics As New List(Of PictureBox)
        Private Range As Integer = 4 'grid squares
        Private Msg As String
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            ClientSize = New Size(400, 400)
    
            'get the ratio of pixels to row and column grid size
            ScaleRatio = ClientSize.Width / GridColsRows.X
            '= 400 pixels form client width / 8 cols = 50 pixels per column
    
            'locate and size the pictureboxes in form client pixels
            picRed.Location = GetClientPixelPtFromGridCRPt(PicRedCRGridPt)
            picRed.Size = GridSquareSizePixels
            picRed.BackColor = Color.Red
            picRed.Name = "Red Picturebox"
    
            picBlue.Location = GetClientPixelPtFromGridCRPt(PicBlueCRGridPt)
            picBlue.Size = GridSquareSizePixels
            picBlue.BackColor = Color.Blue
            picBlue.Name = "Blue Picturebox"
    
            picGreen.Location = GetClientPixelPtFromGridCRPt(PicGreenCRGridPt)
            picGreen.Size = GridSquareSizePixels
            picGreen.BackColor = Color.Green
            picGreen.Name = "Green Picturebox"
    
            picWhite.Location = GetClientPixelPtFromGridCRPt(PicWhiteCRGridPt)
            picWhite.Size = GridSquareSizePixels
            picWhite.BackColor = Color.White
            picWhite.Name = "White Picturebox"
        End Sub
    
        Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.Clear(Color.Black)
    
            'grid
            Using br As New SolidBrush(Color.Gray),
                    f As New Font("arial", 12)
    
                'grid
                For x = 0 To GridColsRows.X * GridSquareSizePixels.Width Step GridSquareSizePixels.Width
                    e.Graphics.DrawLine(Pens.Gray, x, 0, x, ClientSize.Height)
                    e.Graphics.DrawString(CInt(x / GridSquareSizePixels.Width).ToString, f, br, x, 0)
                Next
    
                For y = 0 To GridColsRows.Y * GridSquareSizePixels.Height Step GridSquareSizePixels.Height
                    e.Graphics.DrawLine(Pens.Gray, 0, y, ClientSize.Width, y)
                    e.Graphics.DrawString(CInt(y / GridSquareSizePixels.Height).ToString.ToString, f, br, 0, y)
                Next
    
                If SelectedPic IsNot Nothing Then
                    'highlight selected
                    Dim rect As Rectangle = SelectedPic.Bounds
                    rect.Inflate(10, 10)
                    e.Graphics.FillEllipse(Brushes.Yellow, rect)
    
                    'hightlight targets
                    For Each t In TargetPics
                        rect = t.Bounds
                        rect.Inflate(10, 10)
                        e.Graphics.DrawEllipse(Pens.SkyBlue, rect)
    
                    Next
    
                    'range
                    Dim w As Integer = GridSquareSizePixels.Width * Range
                    rect = New Rectangle(SelectedPic.Location.X - w, SelectedPic.Location.Y - w, 2 * w, 2 * w)
                    e.Graphics.DrawEllipse(Pens.Green, rect)
                End If
    
                e.Graphics.DrawString(Msg, f, Brushes.White, 20, 20)
            End Using
        End Sub
    
        Private Sub pics_MouseDown(sender As Object, e As MouseEventArgs) Handles _
            picRed.MouseDown, picBlue.MouseDown, picGreen.MouseDown, picWhite.MouseDown
    
            'get a reference to the picture that was clicked
            SelectedPic = DirectCast(sender, PictureBox)
    
            'convert the picture location from form client pixles to grid cols and rows
            Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location)
            Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
    
            'get the distance between upper left of pictureboxes
            Dim angle, distance As Double
            TargetPics.Clear()
    
            For Each ctrl In Me.Controls
                If TypeOf ctrl Is PictureBox Then
                    Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
                    If Not pic.Name = SelectedPic.Name Then
                        'see if this pic within range
                        MathAngleDistanceToPoint(SelectedPic.Location, pic.Location, angle, distance)
                        If distance < (Range * GridSquareSizePixels.Width) Then
                            'this pic is within range
                            Msg &= "   " & pic.Name & " within Range of " & Range & " Squares." & vbLf &
                                "      Angle: " & angle.ToString("f1") & " Deg.   Distance: " & (distance / ScaleRatio).ToString("f1") & " Squares" & vbLf
    
                            TargetPics.Add(pic)
                        End If
                    End If
                End If
            Next
    
            Refresh()
        End Sub
    
        Private Sub MathAngleDistanceToPoint(ByVal ptFrom As PointF, ByVal ptTo As PointF, ByRef angle As Double, ByRef distance As Double)
            'units are pixels and degrees
            'angle 0 is x axis right and increasing clockwise
            Dim dx As Double = ptFrom.X - ptTo.X
            Dim dy As Double = ptFrom.Y - ptTo.Y
            angle = 180 + CSng(57.3 * Math.Atan2(dy, dx))
            If angle > 360 Then angle -= 360
            If angle < 0 Then angle += 360
            distance = Math.Sqrt((dx * dx) + (dy * dy))
        End Sub
    
        Private Function GetGridCRPtFromClientPixelPt(clientpixelsPt As Point) As Point
            'convert pixels to columns and rows
            Dim cols As Integer = CInt(clientpixelsPt.X / ScaleRatio)
            Dim rows As Integer = CInt(clientpixelsPt.Y / ScaleRatio)
            Return New Point(cols, rows)
        End Function
    
        Private Function GetClientPixelPtFromGridCRPt(GridCRPt As Point) As Point
            'convert from columns rows to pixels
            Dim x As Integer = CInt(GridCRPt.X * ScaleRatio)
            Dim y As Integer = CInt(GridCRPt.Y * ScaleRatio)
            Return New Point(x, y)
        End Function
    End Class




    • Edited by tommytwotrain Tuesday, April 4, 2017 5:43 PM correct 57.8 to 57.3
    • Marked as answer by Waliur Rahman Thursday, April 6, 2017 2:52 PM
    • Unmarked as answer by Waliur Rahman Tuesday, April 11, 2017 2:40 PM
    Tuesday, April 4, 2017 3:47 PM
  • Im in college at the moment just to let you know and I haven't studied A level maths just the standard rve everyone needs to pass

    WRA

    Tuesday, April 4, 2017 4:45 PM
  • Ill analyse this example as soon as possible and understand what is going on here before implementing the intended function

    WRA

    Tuesday, April 4, 2017 4:47 PM
  • Ill analyse this example as soon as possible and understand what is going on here before implementing the intended function

    WRA

    Ok. I just made a correction to the example.

    There are probably other ways to achieve the same thing. Counting squares somehow. If you add rows + cols = distance that will give you a square range area etc. But...

    Maybe Acamar knows another way?

    Tuesday, April 4, 2017 5:48 PM
  • Hopefully but if not, I will try use this example here and possibly manipulate to show the gridcolumn without showing angles and stuff

    WRA

    Tuesday, April 4, 2017 6:12 PM
  • @Tommy

    Thanks again Tommy for the example it is very helpful and given me a look of how I could use range maybe by showing a wide circle as shown from your screenshots. Perhaps that is what I am looking for. 

    For example this game here "Clash of Clans" you can tell the range of it by selecting the defense and it shows a wide circle to show the perimeter which it can attack within. I think I just found my answer Tommy. Your 2nd example could help give me how I could set the range of tanks. 


    WRA

    Tuesday, April 4, 2017 6:22 PM
  • Ohhh thanks for clarifying that Acamar there goes me again assuming other people know what I am on about

    To avoid other people having to guess, just describe what you mean.  What is the distance represented by '5 squares'?  Is it a straight line that crosses over no more than 5 squares?  Is it no more than a total of 5 squares across plus 5 squares up or down?  Is it a length equal to or less than the 5 times the width of a square, or less than or equal to 5 times the diagonal of a square?  Is it any square that can be reached in no more than 5 moves of one square each, and if so are these only horizontal and vertical moves, or are diagonal moves allowed?  Or is it something else? 

    If you draw a grid using pencil and paper, and mark all the squares that are '5 squares' away from the centre square, what is the rule that you used to mark them?  If you can describe that process then you can probably code it.


    • Edited by AcamarMVP Tuesday, April 4, 2017 11:44 PM fmt
    Tuesday, April 4, 2017 10:35 PM
  • Now that you put it into description I am thinking that isn't what I was looking for, the screenshots of the game clash of clans and the example tommy gave is what I am surely looking for and I just have to configure it into the context of my game and then I can proceed to the next stage but unto then I will first understand the example and look at what I want to take out from there

    WRA

    Wednesday, April 5, 2017 11:15 AM
  • @Tommy 

    All I need to do in your example is look at how you have shown the big wide circle with a range of "4" for instance and indicate which tanks are within the given range as you have demonstrated. 


    WRA

    Wednesday, April 5, 2017 12:45 PM
  • Figured it out now I just gotta manipulate in a way to show the range in the context of my game and show the attack happening. Thanks again Tommy and Acamar for the help (especially clarifying)

    In English before coding what I want to do is on the battle field I would select one tank it would show the given range in a circle as seen from the game screenshot which could show what is sitting within its range then if anything is within the perimeter of the circle then it is vulnerable to an attack.


    WRA

    Wednesday, April 5, 2017 12:49 PM
  • @Tommy 

    Afternoon I hope you are well, I am implementing the code to show the range of the tanks but I am getting this error here in the function.

    Just before it could carry out any of the other function such as the aim mode or moving the tanks around


    WRA

    Wednesday, April 5, 2017 3:03 PM
  • @Tommy 

    Afternoon I hope you are well, I am implementing the code to show the range of the tanks but I am getting this error here in the function.

    Just before it could carry out any of the other function such as the aim mode or moving the tanks around


    WRA

    Apparently ScaleFactor is zero. So you get a divide by zero error. Put your mouse pointer over the variable and VS will show the value.

    Note in the example how scalefactor is declared at form level and it is calculated for the defined grid and used throughout to convert from pixels to grid rows and columns and reverse.

    So you need the declare scalefactor and you need to set the value when you define the grid and the size of the client view window ie form.

    Wednesday, April 5, 2017 3:53 PM
  • @Tommy 

    I see thanks for indicating that tommy and another question, your project goes by the references of just squares would I have to do anything different in my project to refer it to my sprites


    WRA

    Wednesday, April 5, 2017 6:45 PM
  • @Tommy

    Ill sleep on this task since I have seen from past few months when I think about it away from the screen, I then find the solution right away and it formulates perfectly the idea solution to the given problem.


    WRA

    Wednesday, April 5, 2017 8:02 PM
  • @Tommy 

    Afternoon, I am getting the problem that some of the code implemented isn't part of a class here is the following it shows. 

    Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
            TargetSprite = DirectCast(Sender, PictureBox).Name
    
    Dim crpt As Point = GetGridCRPtFromClientPixelPt(TargetSprite.Location)here
            Msg = Sprites.Name ("Error here") & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
            Dim distance As Double
            Sprites.Clear()
    
            For Each ctrl In Me.Controls
                    If TypeOf ctrl Is PictureBox Then
                        Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
                 If Not pic.Name = Sprites.Name Then "here"
                        'see if this pic within range
                        If distance < (Range * GridSize.Width) Then
                            'this pic is within range
                            Msg &= "   " & pic.Name & " within Range of " & Range & " Squares." & vbLf & " Squares" & vbLf
                            Sprites.Add(pic) "here"
    End If
                    End If
                End If
                Next
            Refresh()
        End Sub

    Error on the Dim crpt specifically in the section "targetSprite.Location" it says "location isnt a member of string"

    second one is at the msg line under the dim it says "name is not a member of list(of form1.sprite)". Same applies to the if not pic.name statement. 

    Last error comes at Sprites.Add(pic) it says "value of type picturebox cannot be converted to form1.sprite

    Private Class Sprite Public Name As String Public Pic As PictureBox Public ColRow As Point Public Steps As Integer = -1 '-1 = unlimited move Public Visible As Integer = 2 '0 = not visible 1= Frozen 2=Active Public Range As Integer Public AP As Integer Public HP As Integer End Class Private Sprites As New List(Of Sprite) Private MouseDownSprite As Sprite 'Here is the class and some of the declared variables

     Private AttackSprite, TargetSprite As String
        Private TankSelectStatus As Integer = 0

        Private Scaleratio As Double
        Private Msg As String
        Private Range As Integer = 4



    WRA

    Thursday, April 6, 2017 1:20 PM
  • @Tommy 

    Afternoon, I am getting the problem that some of the code implemented isn't part of a class here is the following it shows. 

    Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
            TargetSprite = DirectCast(Sender, PictureBox).Name
    
    Dim crpt As Point = GetGridCRPtFromClientPixelPt(TargetSprite.Location)here
            Msg = Sprites.Name ("Error here") & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
            Dim distance As Double
            Sprites.Clear()
    
            For Each ctrl In Me.Controls
                    If TypeOf ctrl Is PictureBox Then
                        Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
                 If Not pic.Name = Sprites.Name Then "here"
                        'see if this pic within range
                        If distance < (Range * GridSize.Width) Then
                            'this pic is within range
                            Msg &= "   " & pic.Name & " within Range of " & Range & " Squares." & vbLf & " Squares" & vbLf
                            Sprites.Add(pic) "here"
    End If
                    End If
                End If
                Next
            Refresh()
        End Sub

    Error on the Dim crpt specifically in the section "targetSprite.Location" it says "location isnt a member of string"

    second one is at the msg line under the dim it says "name is not a member of list(of form1.sprite)". Same applies to the if not pic.name statement. 

    Last error comes at Sprites.Add(pic) it says "value of type picturebox cannot be converted to form1.sprite

    Private Class Sprite Public Name As String Public Pic As PictureBox Public ColRow As Point Public Steps As Integer = -1 '-1 = unlimited move Public Visible As Integer = 2 '0 = not visible 1= Frozen 2=Active Public Range As Integer Public AP As Integer Public HP As Integer End Class Private Sprites As New List(Of Sprite) Private MouseDownSprite As Sprite 'Here is the class and some of the declared variables

     Private AttackSprite, TargetSprite As String
        Private TankSelectStatus As Integer = 0

        Private Scaleratio As Double
        Private Msg As String
        Private Range As Integer = 4



    WRA

    This code does not work because you are casting the name of the picturebox, a string, to the variable TargetSprite, a string. But then you try to get the TargetSprite.Location from a string. That wont work. And that is what the error msg tell you. You are trying to equate a string to a picturebox class object. Not the same.


         Private AttackSprite, TargetSprite As String

         TargetSprite = DirectCast(Sender, PictureBox).Name

         Dim crpt As Point = GetGridCRPtFromClientPixelPt(TargetSprite.Location)


    In the example the code is:

         SelectedPic = DirectCast(sender, PictureBox)

    so Selectedpic references a picturebox object.

    Next look at the example again. You can see it first gets the target picturebox and references it to the Pic variable. So the variable Pic is a picturebox Class Object. That is not a string. Then the example uses Pic.Name, a string. Then the example compares the pic.name string to the selectedpic.name string. Selectedpic is also a reference to the picturebox class object. Its name is also selectedpic.name as string. So this works when a string is compared to a string as in the example:


            Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
            If Not pic.Name = SelectedPic.Name Then

    In the example I did not save the selected object by saving the index in the sprite list as our other examples, I saved the actual object, a picturebox class object. There is no sprite list in the example. There is no sprite class. There is just the picturebox class in this example.


    Thursday, April 6, 2017 1:46 PM
  • Ohhhhh so In my context for the game wherever the error happens it should be originally to what it was accordingly which was basically the three errors were different so reverse them to their original states

    WRA

    Thursday, April 6, 2017 1:50 PM
  • Ill step through the code to see if there is any particular error or if any pieces of code are skipped.

    WRA

    Thursday, April 6, 2017 1:52 PM
  • @Tommy

    Alrighty it is working now, I just gotta do the paint section so I can see if it is actually doing what I intend on it doing.

    Thanks again bro.


    WRA

    Thursday, April 6, 2017 1:55 PM
  • @Tommy

    Alrighty it is working now, I just gotta do the paint section so I can see if it is actually doing what I intend on it doing.

    Thanks again bro.


    WRA

    Ok.


    PS in the example we search all the controls onthe form and if a control is a picturebox we then see if the Pic (target) is within range of the SelectedPic (attacker) and if so adds that target picturebox to the NEW target list:

        TargetPics.Add(pic)

    In your code you have:

         Sprites.Add(pic) "here"

    That is not what you want. Sprites in your game is a list of all the sprites in the game. You dont want to add targets to that list.

    What you want is to make a new seperate list of the picturebox targets (or sprites in your game) that are within range. Thats what TargetPics is in the example. A list of the target pictureboxes within range of the attacker.

    And in your game once you decide the picturebox is within range you need to decide which sprite has that picturebox.

    Thursday, April 6, 2017 2:06 PM
  • PSS In your game you can search all the sprites in the sprite list to see which are in range. Instead of searching all the pictureboxes on the form as in my example.

    Instead of this:

            For Each ctrl In Me.Controls
                If TypeOf ctrl Is PictureBox Then
                    Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
                    If Not pic.Name = SelectedPic.Name Then


    You can:

           dim targetSpriteList as new List(of Sprite)

            For Each spt as Sprite in Sprites

                    If Not spt.Name = SelectedPic.Name Then

                          if spt is in range

                                targetSpriteList.add(spt)

    I just made a list of targetpics in the example because we don't have a sprite class in the example.

    PS SelectedPic would be SelectedSprite in your game (attacker) for the above code.

    Or you can use spt.pic.name if you compare a sprites picturebox ref to a normal picturebox class. ie if you have a picturebox for SelectedPic then you:

          If Not spt.pic.Name = SelectedPic.Name Then

    But if you figure which sprite has the picturebox sender then you can:

          If Not spt.Name = SelectedSprite.Name Then

    Thursday, April 6, 2017 2:45 PM
  • I see thanks again T 

    You have a good day. This problem has been officially solved. I will announce the next obstacle tomorrow because I am not feeling well: soar throat and runny nose. Worst nightmare in the best time of year, spring. 

    why does life do this to me? :(


    WRA

    Thursday, April 6, 2017 2:52 PM
  • @Tommy 

    I realised I set the private range to apply for all the given sprites, how can I apply it to each sprite "individually" from the sprite class such as one sprite has the range = 1 and another range = 4 


    WRA

    Thursday, April 6, 2017 3:25 PM
  • @Tommy 

    I realised I set the private range to apply for all the given sprites, how can I apply it to each sprite "individually" from the sprite class such as one sprite has the range = 1 and another range = 4 


    WRA


    Set the value the same way you do any other class property. Like

        label1.text = "This text".

    At some proper time and place in the code set the value. When you set it depends on the exact code details.

    Set the range in the form load or whenever you set the other properties for the other sprites like the name and image and picturebox. Or at runtime use an event where the value changes ie

        sprites(spriteindex).range = 4.

    Thursday, April 6, 2017 8:02 PM
  • You should make some functions to do common things. In the red green blue example last thread we used the sprite name and sprite list index you can make functions to do these things:

       Private Function GetSpriteListIndexFromName(theName As String) As Integer
            For i = 0 To PicEXList.Count - 1
                If theName = PicEXList(i).Pic.Name Then
                    Return i
                End If
            Next

            Return -1
        End Function


    If you use the actual class object you dont need the name or list index. Notice this one returns a class object.


        Private Class PicEX
            Public Name As String
            Public Pic As PictureBox
        End Class
        Private PicEXList As New List(Of PicEX)

        Private Function GetSpriteFromName(theName As String) As Sprite
            For i = 0 To PicEXList.Count - 1
                If theName = PicEXList(i).Pic.Name Then
                    Return PicEXList(i)
                End If
            Next

            Return Nothing
        End Function

    The name of the class is just the string name. The sprite list index is just the position in our list of sprites (and changes as sprites are removed). The class object is the complete object. ie The car name is Corvette. The class object is car.

    PS


    Hey get the car!

    Which Car?

    The Corvette!

    Which parking space is the Corvette in?

    33!

    Thursday, April 6, 2017 9:00 PM
  • Like the context you put it into and thanks tommy for the help. I understand and as soon as I get on my laptop next I know how I can solve the problem and if not I know where to leave a msg

    WRA

    Friday, April 7, 2017 11:14 AM
  •      

    'Upgrade TargetSprite = DirectCast(Sender, PictureBox).Name Dim targetSpriteList As New List(Of Sprite) Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location) Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf Dim distance As Double Sprites.Clear() If e.Button = MouseButtons.Right Then For Each spt As Sprite In Sprites If TypeOf Sender Is Sprite Then Dim pic As PictureBox = DirectCast(Sender, PictureBox) If Not spt.Name = SelectedPic.Name Then If distance < (Range * GridSize.Width) Then Msg &= " " & pic.Name & " within Range of " & Range & " Squares." & vbLf targetSpriteList.Add(spt) End If End If End If Next End If Refresh() End Sub

    Afternoon, the new code that I have designed is referring the sprites according to "pic 1, pic 2" etc and I can show when the tanks are in range what If I move the sprites about will it detect if it is out of range?


    WRA

    Friday, April 7, 2017 2:01 PM
  •      

    'Upgrade TargetSprite = DirectCast(Sender, PictureBox).Name Dim targetSpriteList As New List(Of Sprite) Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location) Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf Dim distance As Double Sprites.Clear() If e.Button = MouseButtons.Right Then For Each spt As Sprite In Sprites If TypeOf Sender Is Sprite Then Dim pic As PictureBox = DirectCast(Sender, PictureBox) If Not spt.Name = SelectedPic.Name Then If distance < (Range * GridSize.Width) Then Msg &= " " & pic.Name & " within Range of " & Range & " Squares." & vbLf targetSpriteList.Add(spt) End If End If End If Next End If Refresh() End Sub

    Afternoon, the new code that I have designed is referring the sprites according to "pic 1, pic 2" etc and I can show when the tanks are in range what If I move the sprites about will it detect if it is out of range?


    WRA


    You need to create one sub routine that does a desired task.

    In this case the task is get the objects in range of a selected object. So make a GetObjectsInRange(selectedobject) sub. Now anytime you need to update the objects in range call the sub and then redraw the screen. So in mousedown and mouseup you can call the routine and then picturebox.refresh.

    In your game I guess after dragging the object, in the mouseup event, you call GetObjectsInRange and the refresh.

    So take the code you have now required to do the task out of the mousedown event and replace it with a sub routine. Only the code for the update range task. Then call it where needed.

    Make the sub routine so it can be called easily, pass any variables required, etc.

    Friday, April 7, 2017 5:55 PM
  • Afternoon, the new code that I have designed is referring the sprites according to "pic 1, pic 2" etc and I can show when the tanks are in range what If I move the sprites about will it detect if it is out of range?

    You need to be quite confident that you have got the basics under control.  For instance, you need to be sure that you can see the problem with this code:

            Sprites.Clear()
    
            If e.Button = MouseButtons.Right Then
                For Each spt As Sprite In Sprites
    
    and that you appreciate the inconsistencies in this code:
            TargetSprite = DirectCast(Sender, PictureBox).Name
            ... 
            If e.Button = MouseButtons.Right Then
                For Each spt As Sprite In Sprites
                    If TypeOf Sender Is Sprite Then

    Otherwise you are just throwing random bits of code at the problem, and that is simply not going to work.   You should be able to describe, in plain language, what each line of code is actually intended to do, so that you can construct your sequence of actions that accomplish a particular task.

    Friday, April 7, 2017 9:21 PM
  • @Acamar 

    Very helpful Acamar so rather than cut to the chase take the slow approach of what the piece of code is suppose to do. 

    First, as soon as I select the right mouse button, it will display a circle which shows the tank is highlighted then another circle to show the range.

    Second the if statement to actually indicate whether or not a tank is in range, if there is a tank within the given perimiter then the attacking tank will be able to attack it otherwise the attack will miss. 


    WRA

    Saturday, April 8, 2017 2:19 PM
  • I just found tommy that If I chained the tank range and attack features together it creates an overload error for the program so Is it possible to fit both as a doubleclick mouse event lmb and rmb for each?

    WRA

    Saturday, April 8, 2017 2:51 PM
  • @Tommy 

    Hi I have created a sub "GetObjectsInRange", could you possibly make a rough structure of how it should be written I find when a structure and comments are within a sub I find it easy to understand and know what I have to put into it. 


    WRA

    Saturday, April 8, 2017 2:57 PM
  • @Tommy 

    Hi I have created a sub "GetObjectsInRange", could you possibly make a rough structure of how it should be written I find when a structure and comments are within a sub I find it easy to understand and know what I have to put into it. 


    WRA

    Sorry, I dont really know what you are talking about in the previous question.

    You will need to show a code example and describe the problem including any errors you now get and their location for anyone to understand the question. I dont know what you mean by "chaining" exactly so you need to show it with code I guess best you can. Maybe a new question. You get others interested then with more ideas etc.

    I would avoid the double click event it can be goofy and hard to sort from other events at times. Just stick to mousedown, mousemove, mouseup thats all you need for now.

    To make a GetObjectsInRange sub, first get the code you have working. Then just copy the part you need to use more than once into a sub routine. I have shown all I can without having your existing code and doing it for you.

    You know the routine. You try it. You post what you have done with any questions such that we can understand.

    Does your code run now or is it just a bunch of errors?

    Until I see a that you have a working something from the previous thread I am not going much further. As Acamar pointed out it still seems the last questions are hanging.


    Saturday, April 8, 2017 3:28 PM
  • Sounds like a good idea tommy the range isnt workig effectively because the attack feature is in the double click and range in there is messing up my code sso Should I use keypress such as select R on the keyboard instead?
    Saturday, April 8, 2017 4:33 PM
  • Sounds like a good idea tommy the range isnt workig effectively because the attack feature is in the double click and range in there is messing up my code sso Should I use keypress such as select R on the keyboard instead?

    "First, as soon as I select the right mouse button, it will display a circle which shows the tank is highlighted then another circle to show the range."


    Ok so you rmb mousedown on the attack tank picturebox? Draw the range circle for the attack tank.

    "Second the if statement to actually indicate whether or not a tank is in range, if there is a tank within the given perimiter then the attacking tank will be able to attack it otherwise the attack will miss. "


    Now you draw circles around the tank pictureboxes in range of the attack tank.


    Now what happens? Click a button or what?

    Is that what you mean? Press Ctrl+R ? What would happen then?

    Oh, I see... so what you do now is double click the picture box to select the target tank? Thats what you have now. That should work I think. You just have to work out the details.

    In the double click event you do the task: Delete target tank, update graphics.

    Or in the keydown event you do the task.

    Or RMB Click  the tank picturebox.

    Or click a Fire button on the attack tank control panel.

    Even all of the above.

    Sometimes you need to try them to see how it all operates together from start to finish. Maybe this and that is awkward. That and the other is better.

    Saturday, April 8, 2017 5:50 PM
  • Like somebody mentioned in the forum "trial and error method"

    WRA

    Saturday, April 8, 2017 5:52 PM
  • @Tommy, Acamar and others

    I'll open a new thread for this so this can continue on there sound good?


    WRA

    Saturday, April 8, 2017 6:05 PM
  • @Tommy, Acamar and others

    I'll open a new thread for this so this can continue on there sound good?


    WRA

    If you want.

    You still need another flag that tells you when the attacker has been selected and now awaiting selection of the target.

    Otherwise a player can double click something and it blows up. Right?

    So you have say: gamedata.attackstage = 1 when the attacker is selected. Then in the select target event like double click you

       If gamedata.attackstage = 1 then
            Delete target
            gamedata.attackstage = 0
            Update Graphics

    You can also do it using rmb mousedown if you want. Rmb click attacker pic setflag=1, rmb target pic if setflag=1 then do task.

    Thats how you sort it out as the action progressess you set flags increment counters save data update graphics, repeat. That is the basic game loop.

    Saturday, April 8, 2017 6:16 PM
  • @Tommy 

    I just realised and forgot, why didn't I ask you this beforehand that could be why it is calling some of my sprites "Pic 0" etc.

    the following declared variables in your program in comparison to my program. 

    'My version
    
     Private AttackSprite, TargetSprite As String
        Private TankSelectStatus As Integer = 0
        Private Range As Integer = 4 'grid squares
        Private Msg As String
        Private Scaleratio As Double
    
        Private Sprites As New List(Of Sprite)
        Private MouseDownSprite As Sprite
    
        Private Class Sprite
            Public Name As String
            Public Pic As PictureBox
            Public ColRow As Point
            Public Steps As Integer = -1    '-1 = unlimited move
            Public Visible As Integer = 2   '0 = not visible  1= Frozen  2=Active   
            Public Range As Integer
            Public AP As Integer
            Public HP As Integer
        End Class
    
    'Your version
    
    Private WithEvents picRed As New PictureBox With {.Parent = Me}
        Private WithEvents picBlue As New PictureBox With {.Parent = Me}
        Private WithEvents picGreen As New PictureBox With {.Parent = Me}
        Private WithEvents picWhite As New PictureBox With {.Parent = Me}
        Private GridColsRows As New Point(8, 8)
        Private GridSquareSizePixels As New Size(50, 50)
        Private ScaleRatio As Double  'pixels per column
        Private PicRedCRGridPt As New Point(2, 3)  'pic location in grid (column,rows)
        Private PicBlueCRGridPt As New Point(5, 4)
        Private PicGreenCRGridPt As New Point(3, 6)
        Private PicWhiteCRGridPt As New Point(0, 6)
        Private SelectedPic As PictureBox
        Private TargetPics As New List(Of PictureBox)
        Private Range As Integer = 4 'grid squares
        Private Msg As String
    Look to where you have called your picturebox in comparison to mine 

    Private SelectedPic As PictureBox
        Private TargetPics As New List(Of PictureBox)

    Private Sprites As New List(Of Sprite)

    Public Pic As PictureBox

    I found both your declared variables are within one class whilst my sprite: one is in the main form whilst the other is in the sprite class. Could this be the answer to majority of the questions? Why it wouldn't call it by its name nor indicate the range.


    WRA

    Saturday, April 8, 2017 6:20 PM
  • @Tommy 

    I just realised and forgot, why didn't I ask you this beforehand that could be why it is calling some of my sprites "Pic 0" etc.

    the following declared variables in your program in comparison to my program. 

    'My version
    
    Look to where you have called your picturebox in comparison to mine 

    Private SelectedPic As PictureBox
        Private TargetPics As New List(Of PictureBox)

    Private Sprites As New List(Of Sprite)

    Public Pic As PictureBox

    I found both your declared variables are within one class whilst my sprite: one is in the main form whilst the other is in the sprite class. Could this be the answer to majority of the questions? Why it wouldn't call it by its name nor indicate the range.


    WRA

    Yes of course it makes a difference. The two code examples do different things were setup different ways and used different names. They were not meant to mix directly just examples of one thing.

    I told you a thousand times already there are a thousand ways to do something!  :)

    Its just that the Rube Goldberg machine you put together in code has to work and do that task.

    Look over the threads again Acamar keeps pointing out the same thing too. Thats why this range example I did not make a list of sprites. So you will maybe see these details before the sprite class is added. I named the pics red green blue. Could be Roy, George, Bob.

    Yes in the range example there is no sprite class. So I refer to the picturebox.name

    Or I just used a reference to the picturebox class object.

              dim selectedpicturebox as picturebox = picturebox1

    In the examples with the sprite class we can refer to the picturebox1.name,

    or to the Sprite.pic.name which is the same thing as the picturebox1.name.

    Or we can refer to the sprite.name, not the same as the sprite.pic.name.

    So yeah there it is the things in your code are not mixed together quite right and using the wrong names and etc.

    That's why you should do one thing at a time. Get just the first thing working. Then add to it. Debug it.

    You need to be able to run it and do one thing. If all you have is a list of hundreds of errors and you cant run its hard to debug. You should remove the parts that don't work until you have a working shell.

    Does your project code run now or is it just a bunch of errors?

    Saturday, April 8, 2017 6:51 PM
  •      

       
     Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
    
    
    If e.Button = MouseButtons.Right Then
                'Upgrade
                TargetSprite = DirectCast(Sender, PictureBox).Name
    
                Dim crpt As Point = GetGridCRPtFromClientPixelPt(pic.location)
                Msg = AttackSprite.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
    
                Dim distance As Double
                TargetSprite.Clear()
    
                If e.Button = MouseButtons.Right Then
                    For Each ctrl In Me.Controls
                        If TypeOf ctrl Is PictureBox Then
                            Dim pic As PictureBox = DirectCast(ctrl, PictureBox)
                            If Not pic.Name = pic.Name Then
                                If distance < (Range * GridSize.Width) Then
                                    Msg &= "   " & pic.Name & " within Range of " & Range & " Squares." & vbLf
                                    TargetSprite.Add(pic)
                                End If
                            End If
                        End If
                    Next
                End If
            End If
        End Sub
    
    

    First error is the msg under the dim crpt as point variable where it shows attacksprite.name it says "name is not a member of string"

    Second,  "TargetSprite.Clear()" this says clear isn't a member of string 

    Finally, "TargetSprite.Add(pic)" this says add isnt a member of string 

    I presume these solution to problems relatively clear but not to me. 

                                    

    WRA

    Saturday, April 8, 2017 7:49 PM
  • Very helpful Acamar so rather than cut to the chase take the slow approach of what the piece of code is suppose to do.

    Forget about the game action for a moment.  Take a look at the two bits of code I selected from your example.  Explain, for your own information, exactly what the errors are in those bits of code.  There is no point in continuing until you have shown that you understand what is wrong with that code. 

    Saturday, April 8, 2017 9:13 PM
  • Sprites.Clear()
    
            If e.Button = MouseButtons.Right Then
                For Each spt As Sprite In Sprites

    Right the first part above I can see the sprites refresh but no clue why it is at the top that is at the end of the for loop

    Then the if statement detects for a right mouse click then begind the forloop.

    the targetsprite is a directcast to the reference of picturebox according to its name.

    ohhhh. this part here does not make any sense "If TypeOf Sender Is Sprite Then" looks more like waffle

    TargetSprite = DirectCast(Sender, PictureBox).Name ... If e.Button = MouseButtons.Right Then For Each spt As Sprite In Sprites If TypeOf Sender Is Sprite Then


    WRA

    Saturday, April 8, 2017 9:55 PM
  • Sprites.Clear()
    
            If e.Button = MouseButtons.Right Then
                For Each spt As Sprite In Sprites

    Right the first part above I can see the sprites refresh but no clue why it is at the top that is at the end of the for loop

    Then the if statement detects for a right mouse click then begind the forloop.

    the targetsprite is a directcast to the reference of picturebox according to its name.

    ohhhh. this part here does not make any sense "If TypeOf Sender Is Sprite Then" looks more like waffle

    TargetSprite = DirectCast(Sender, PictureBox).Name ... If e.Button = MouseButtons.Right Then For Each spt As Sprite In Sprites If TypeOf Sender Is Sprite Then


    WRA

    You are still looking at this question with too broad a viewpoint.  The overall strategy is not relevant to the issue - it's simply a matter that the code cannot possibly be correct, and I am not sure that you understand why.

    Saturday, April 8, 2017 10:16 PM
  • Sprites.Clear()
    
            If e.Button = MouseButtons.Right Then
                For Each spt As Sprite In Sprites

    Right the first part above I can see the sprites refresh but no clue why it is at the top that is at the end of the for loop

    Then the if statement detects for a right mouse click then begind the forloop.

    the targetsprite is a directcast to the reference of picturebox according to its name.

    ohhhh. this part here does not make any sense "If TypeOf Sender Is Sprite Then" looks more like waffle

    TargetSprite = DirectCast(Sender, PictureBox).Name ... If e.Button = MouseButtons.Right Then For Each spt As Sprite In Sprites If TypeOf Sender Is Sprite Then


    WRA


    What are the tasks?

    Upon mousedown identify which picturebox was clicked.

    If no attacker has been selected then save the current picturebox as the attacker.

    Draw a range circle around the attacker picturebox.

    Determine which of the pictureboxes is within the range circle, not including the attacker. These are the target pictureboxes.

    Save a list of the target pictureboxes.

    Draw circles around all the target pictureboxes in the list.

    Set a flag indicating the attacker has been selected.

    Await further instructions....................

    Upon mousedown, identify which picturebox was clicked.

    If the attacher has been selected then search through the target picturebox list to make sure the current picturebox is in the list of valid targets.

    If the current picturebox is in the target picturebox list then blow it up.

    Delete the current picturebox, set the attacker selected flag to zero, update the graphics.

    Between each of these steps is a few lines of code. Each line does something.  Break the eggs, beat the eggs, cook the eggs, eat the eggs, wash the dish.

    Insert the code for each of the tasks. One task at a time. Understand the code for the task.

    This is a picturebox reference.

    This is the name of the picturebox to whom the reference points.

    Pay attention to the declarations. If you make a list of pictureboxes that is not a string.

    PS If you  Break the eggs, beat the eggs, cook the chicken, eat the eggs, wash the dish, then something went wrong somewhere. Comprende?

    Saturday, April 8, 2017 11:30 PM
  •     Private Sub Pics_Tripleclick(Sender As Object, e As MouseEventArgs)
            'Upgrade
            If e.Button = MouseButtons.Right Then
                SelectedPic = DirectCast(Sender, PictureBox)
    
                Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location)
                Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
    
                Dim distance As Double
                TargetPics.Clear()
    
                For Each ctrl In Controls
                    If TypeOf ctrl Is Sprite Then
                        Dim spt As Sprite = DirectCast(ctrl, Sprite)
                        If Not spt.Name = SelectedPic.Name Then
                            If distance < (Range * GridSize.Width) Then
                                Msg &= "   " & spt.Name & " within Range of " & Range & " Squares." & vbLf
                                TargetPics.Add(spt)
                            End If
                        End If
                    End If
                Next
    
                Refresh()
            End If

    Afternoon Tommy 

    It is like you said one step at a time. So to start it off Lets deal with first why is it referring to as pic 0 pic 1 etc rather than the according name? 

    Then will deal with the range.

    I realise maybe Im going way too fast at this.


    WRA

    Sunday, April 9, 2017 11:35 AM
  •     Private Sub Pics_Tripleclick(Sender As Object, e As MouseEventArgs)
            'Upgrade
            If e.Button = MouseButtons.Right Then
                SelectedPic = DirectCast(Sender, PictureBox)
    
                Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location)
                Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
    
                Dim distance As Double
                TargetPics.Clear()
    
                For Each ctrl In Controls
                    If TypeOf ctrl Is Sprite Then
                        Dim spt As Sprite = DirectCast(ctrl, Sprite)
                        If Not spt.Name = SelectedPic.Name Then
                            If distance < (Range * GridSize.Width) Then
                                Msg &= "   " & spt.Name & " within Range of " & Range & " Squares." & vbLf
                                TargetPics.Add(spt)
                            End If
                        End If
                    End If
                Next
    
                Refresh()
            End If

    Afternoon Tommy 

    It is like you said one step at a time. So to start it off Lets deal with first why is it referring to as pic 0 pic 1 etc rather than the according name? 

    Then will deal with the range.

    I realise maybe Im going way too fast at this.


    WRA

    In this thread your form load code has this setup.


    https://social.msdn.microsoft.com/Forums/vstudio/en-US/b227d68d-b7d6-4e25-8d9c-3d291759b21b/continuation-of-the-last-error-between-mouse-click-to-move-sprite-and-mouseclick-to-attack-sprite?forum=vbgeneral

             For i = 0 To 2
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    Dim ImageFile As String = IO.Path.Combine("N:\Documents\Visual Studio 2015\Projects\Most Recent Testings\", Sprites(i).Name & ".png")
                    'Dim ImageFile As String = IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\", Sprites(i).Name & ".png")
                    .BackgroundImage = Image.FromFile(ImageFile)
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
                Sprites(i).Pic = pic
            Next
    
    
    

    So the line "Pic" & i.ToString is where the names pic 1, pic 2 come from.

    This is the string name of the picture box. It is not the picturebox itself, it is the string name. Just like you have a string name "Waliur". You are the class human. Your human.name is Waliur. But you are you. Your name is just a label someone attached to you. You are the object you named Waliur.

    The sprite class we define is just that, a definition. And then we define that the sprite contains a picturebox. That picturebox can be referenced from the sprite like thissprite.pic (the picture object) or this sprite.pic.name. (the picture name). Or the picture is also the vbcode name maybe Picturebox1. And it can be the name we give it Picturebox1.name which is the same as sprite.pic.name because the picture box in our sprite is picturebox1.

    In the examples we have had lately we have used all these forms of naming or referencing.

    You need to work examples of using a class.

    You can

         dim thispic as picturebox = DirectCast(Sender, PictureBox)

    that makes thispic a reference to the picturebox class object. So thispic is the reference to the object itself.

    Then you can

         dim thispicname as string = DirectCast(Sender, PictureBox).name

    And now you have the string name we assigned to the picturebox in thispicname variable.

    But,    thispic is not equal to thispicname. One is a string and one is a picturebox. Say you have two apples. One green one red. The class is apple, the name is greenapple and redapple.

    So if you ask someone for the redapple, do you they give you the name "redapple" on a piece of paper ie the string name? Or do they give you the real apple object. The two things are not the same. One is the name of the object redapple on a piece of paper and one is the actual object named redapple. Not the same.

    In your example if you are using the setup code shown then when you


         dim thispic as picturebox = DirectCast(Sender, PictureBox)

    thispic variable is a reference to the actual picturebox object.


    and when you


        dim thispicname as string = DirectCast(Sender, PictureBox).name

    thispicname is a string containing the name of the picturebox ie Pic 1.

    So if you if thispicname = thispic that is an error. One is a string one is a picturebox.

    And if you dim apoint as point = thispicname.location that is an error as string has no location. But if you

         if thispicname = thispic.name then

    that is ok. String to string.

    or

          dim apoint as point = thispic.location

    that is ok because thispic has a location.

    So one can do it any of the ways. Use the .name or use the sprite list reference to the pic. But one must always compare apples to apples and oranges to oranges.

             DirectCast(Sender, PictureBox).name

             = thispic.name

             = thispicname

             = sprites(thisindex).pic.name

             = "Pic 1"

    its all the same thing just different ways of writing it in code.

    Sunday, April 9, 2017 12:31 PM
  • I see so what you are basically saying here is there isn't anything wrong with the code I just need to look at previous code and see how did I solve the issue there because It was already solved on calling the picturebox to its given names 

    WRA

    Sunday, April 9, 2017 12:41 PM
  • @Tommy 

    I realise I shouldn't confuse the targetsprite and selectedpic make each one perform a separate function. so target sprite and attacksprite already have the feature to attack another sprite so that is fine I will use the selectedpic though ill call it something else and figure on operating that. 


    WRA

    Sunday, April 9, 2017 12:44 PM
  • Quick question tommy. Would I need to create a private function for this in particular or nope?

    WRA

    Sunday, April 9, 2017 12:51 PM
  • I see so what you are basically saying here is there isn't anything wrong with the code I just need to look at previous code and see how did I solve the issue there because It was already solved on calling the picturebox to its given names 

    WRA

    No there is something wrong with your code, it does not work.

    No you need to understand why previous examples work. Then write your own code that also works based on what you have learned in the previous examples.

    It still seems you don't totally understand the original example you started with from this thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3401f9f4-bfab-4586-ad47-f80c8bf1f46d/move-a-picturebox-along-a-game-grid?forum=vbgeneral

    So maybe you should go back and understand why the pics were named as they were and then understand you can name them however you want that works. Then you can understand how to add to the original example and modify it for your exact needs. Then you can understand why something's in the example need to be changed for your game.

    Sunday, April 9, 2017 1:00 PM
  • Alright sounds like a good idea I will find the last example and look at how I with pointers manage to figure how i got it fully up and running. That is surely the core to the problem I am facing right now and hopefully the rest will unfold itself

    WRA

    Sunday, April 9, 2017 1:09 PM
  • I see so what you are basically saying here is there isn't anything wrong with the code I just need to look at previous code and see how did I solve the issue there

    On the contrary.  There are serious problems with the code, arising from the fact that you are not looking closely enough at what the code actually does.  This example

                Dim distance As Double
                TargetPics.Clear()
                For Each ctrl In Controls
                    If TypeOf ctrl Is Sprite Then
                        Dim spt As Sprite = DirectCast(ctrl, Sprite)
                        If Not spt.Name = SelectedPic.Name Then
                            If distance < (Range * GridSize.Width) Then
    

    contains two very elementary errors which indicate that you are not understanding some basic concepts. 

    You need to ask yourself:

    1. What will the result of the 'If TypeOf ...' test be?   See:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection(v=vs.110).aspx

    2. What will the result of the 'If distance < ...' test be?

    Sunday, April 9, 2017 9:28 PM
  • Very good question to point out there Acamar. I think the type of isnt beneficial for my sprite feature, idk about ctrl. I realised when I call the sprite class from the last example to do with attacking another sprite it can call it according to its name but for this one nope. Im gonna figure that one out today im sure the solution is clear as day

    WRA

    Tuesday, April 11, 2017 8:09 AM
  • Very good question to point out there Acamar. I think the type of isnt beneficial for my sprite feature

    It's not a question of 'beneficial' or not.  The problem is that the code is just plain incorrect. 

    It wouldn't work regardless of what you are trying to do.  Sprite is a class that you defined for the purpose of collecting your sprite information together under a single object and (presumably) implementing some sprite-like actions, like moving, shooting or getting destroyed.  A Sprite object instance could never be a member of the Controls collection because it isn't a control, so that loop could never actually do anything.

    There doesn't seem to be any connection between what you want to actually do at that point, and the code that you are creating.  And I believe that is because you do not have an adequate description of the task - you are still not thinking at the appropriate level of detail. You are responding to these basic coding problems with generalisations.

    Tuesday, April 11, 2017 9:43 AM
  • I see so to do with the class sprite it has to link towards my piece of code that I have here? This is something I was thinking about

    WRA

    Tuesday, April 11, 2017 10:13 AM
  • I see so to do with the class sprite it has to link towards my piece of code that I have here?

    Have you considered my other question "What will the result of the 'If distance < ...' test be?" and why that pieces of code cannot work?

    Tuesday, April 11, 2017 10:51 AM
  • I took a look at it and yes realised that will act dysfunctionally, it will act on the bigger picture rather than each sprite alone. 

    WRA

    Tuesday, April 11, 2017 11:40 AM
  • @Acamar or Tommy

    If I am to use the range code I presume it needs to operate alongside the attack and target sprite feature


    WRA

    Tuesday, April 11, 2017 11:43 AM
  • @Acamar or Tommy

    If I am to use the range code I presume it needs to operate alongside the attack and target sprite feature


    WRA

    Get the first part working and understood (selecting attacker) before moving to the second part (selecting target).

    So here is the same example with a list of sprites class. Does the same thing as the other example but I have only used the sprite class objects, not the string names of things.

    Also in this example I just searched the list of sprites to see whats in range not the controls on the form. I only used the controls because that example did not have a list of sprites. So instead I used the list of controls that are on the form but that is more difficult and one of the reasons we make the Sprite class to help collect things and make the coding easier.

    However either way works fine. There are hundreds of ways to do something.

    The names and methods are slightly different in this example so you cant just copy this code without making changes to fit your project exactly.

    See our list of sprites in the image and each sprite has a string name and a picturebox class object reference.

    Public Class Form4
        Private WithEvents picRed As New PictureBox With {.Parent = Me}
        Private WithEvents picBlue As New PictureBox With {.Parent = Me}
        Private WithEvents picGreen As New PictureBox With {.Parent = Me}
        Private GridColsRows As New Point(8, 8)
        Private GridSquareSizePixels As New Size(50, 50)
        Private ScaleRatio As Double  'pixels per column
        Private PicRedCRGridPt As New Point(2, 3)  'pic location in grid (column,rows)
        Private PicBlueCRGridPt As New Point(5, 4)
        Private PicGreenCRGridPt As New Point(3, 6)
        Private SelectedPic As PictureBox
        Private TargetSprites As New List(Of Sprite)
        Private Range As Integer = 4 'grid squares
        Private Msg As String
        Private Class Sprite
            Public Name As String
            Public Pic As PictureBox
        End Class
        Private Sprites As New List(Of Sprite)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DoubleBuffered = True
            ClientSize = New Size(400, 400)
    
            'get the ratio of pixels to row and column grid size
            ScaleRatio = ClientSize.Width / GridColsRows.X
            '= 400 pixels form client width / 8 cols = 50 pixels per column
    
            'locate and size the pictureboxes in form client pixels
            Dim spt1 As New Sprite
            picRed.Location = GetClientPixelPtFromGridCRPt(PicRedCRGridPt)
            picRed.Size = GridSquareSizePixels
            picRed.BackColor = Color.Red
            picRed.Name = "Red Picturebox"      'this is the string name for the picturebox
            spt1.Pic = picRed                    'this is the picturebox control
            spt1.Name = "Sprite Red"             'this is the string name for the sprite
            Sprites.Add(spt1)
    
            Dim spt2 As New Sprite
            picBlue.Location = GetClientPixelPtFromGridCRPt(PicBlueCRGridPt)
            picBlue.Size = GridSquareSizePixels
            picBlue.BackColor = Color.Blue
            picBlue.Name = "Blue Picturebox"
            spt2.Pic = picBlue                   'this is the picturebox control
            spt2.Name = "Sprite Blue"             'this is the string name for the sprite
            Sprites.Add(spt2)
    
            Dim spt3 As New Sprite
            picGreen.Location = GetClientPixelPtFromGridCRPt(PicGreenCRGridPt)
            picGreen.Size = GridSquareSizePixels
            picGreen.BackColor = Color.Green
            picGreen.Name = "Green Picturebox"
            spt3.Pic = picGreen                   'this is the picturebox control
            spt3.Name = "Sprite Green"             'this is the string name for the sprite
            Sprites.Add(spt3)
        End Sub
    
        Private Sub pics_MouseDown(sender As Object, e As MouseEventArgs) Handles _
            picRed.MouseDown, picBlue.MouseDown, picGreen.MouseDown
    
            'get a reference to the picture that was clicked
            SelectedPic = DirectCast(sender, PictureBox)
    
            'convert the picture location from form client pixles to grid cols and rows
            Dim crpt As Point = GetGridCRPtFromClientPixelPt(SelectedPic.Location)
            Msg = SelectedPic.Name & ": Column " & crpt.X & ", Row " & crpt.Y & vbLf
    
            Dim angle, distance As Double
            TargetSprites.Clear()
    
            For Each spt In Sprites
                If Not spt.Pic Is SelectedPic Then
                    'see if this pic within range
                    MathAngleDistanceToPoint(SelectedPic.Location, spt.Pic.Location, angle, distance)
                    If distance < (Range * GridSquareSizePixels.Width) Then
                        'this pic is in range add to the list of sprites in range
                        TargetSprites.Add(spt)
                    End If
                End If
            Next
    
            Invalidate()
        End Sub
    
        Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.Clear(Color.Black)
    
            'grid
            Using br As New SolidBrush(Color.Gray),
                    f As New Font("arial", 12)
    
                'grid
                For x = 0 To GridColsRows.X * GridSquareSizePixels.Width Step GridSquareSizePixels.Width
                    e.Graphics.DrawLine(Pens.Gray, x, 0, x, ClientSize.Height)
                    e.Graphics.DrawString(CInt(x / GridSquareSizePixels.Width).ToString, f, br, x, 0)
                Next
    
                For y = 0 To GridColsRows.Y * GridSquareSizePixels.Height Step GridSquareSizePixels.Height
                    e.Graphics.DrawLine(Pens.Gray, 0, y, ClientSize.Width, y)
                    e.Graphics.DrawString(CInt(y / GridSquareSizePixels.Height).ToString.ToString, f, br, 0, y)
                Next
    
                If SelectedPic IsNot Nothing Then
                    'highlight selected
                    Dim rect As Rectangle = SelectedPic.Bounds
                    rect.Inflate(10, 10)
                    e.Graphics.FillEllipse(Brushes.Yellow, rect)
    
                    'hightlight targets
                    For Each spt In TargetSprites
                        rect = spt.Pic.Bounds
                        rect.Inflate(10, 10)
                        e.Graphics.DrawEllipse(Pens.SkyBlue, rect)
                    Next
    
                    'range circle
                    Dim w As Integer = GridSquareSizePixels.Width * Range
                    rect = New Rectangle(SelectedPic.Location.X - w, SelectedPic.Location.Y - w, 2 * w, 2 * w)
                    e.Graphics.DrawEllipse(Pens.Green, rect)
                End If
    
                e.Graphics.DrawString(Msg, f, Brushes.White, 20, 20)
            End Using
        End Sub
    
        Private Sub MathAngleDistanceToPoint(ByVal ptFrom As PointF, ByVal ptTo As PointF, ByRef angle As Double, ByRef distance As Double)
            'units are pixels and degrees
            'angle 0 is x axis right and increasing clockwise
            Dim dx As Double = ptFrom.X - ptTo.X
            Dim dy As Double = ptFrom.Y - ptTo.Y
            angle = 180 + CSng(57.3 * Math.Atan2(dy, dx))
            If angle > 360 Then angle -= 360
            If angle < 0 Then angle += 360
            distance = Math.Sqrt((dx * dx) + (dy * dy))
        End Sub
    
        Private Function GetGridCRPtFromClientPixelPt(clientpixelsPt As Point) As Point
            'convert pixels to columns and rows
            Dim cols As Integer = CInt(clientpixelsPt.X / ScaleRatio)
            Dim rows As Integer = CInt(clientpixelsPt.Y / ScaleRatio)
            Return New Point(cols, rows)
        End Function
    
        Private Function GetClientPixelPtFromGridCRPt(GridCRPt As Point) As Point
            'convert from columns rows to pixels
            Dim x As Integer = CInt(GridCRPt.X * ScaleRatio)
            Dim y As Integer = CInt(GridCRPt.Y * ScaleRatio)
            Return New Point(x, y)
        End Function
    
    End Class

    Tuesday, April 11, 2017 2:00 PM
  • Alright tommy Ill keep taking a shot to understand properly what am I completely doing wrong here. This is complexity reaching its peak and solving this will mean major part is solved. 

    WRA

    Tuesday, April 11, 2017 2:23 PM
  • In summary to the given code above, what the code does is select the given sprite, then it will show the actual name given to it from the form1 load feature and provide the co-ordinates of its position and the perimiter in a circle of its range. Am I right here so far

    WRA

    Tuesday, April 11, 2017 2:30 PM
  •    
      If e.Button = MouseButtons.Left Then
                If TankSelectStatus = 1 Then
                    TargetSprite = DirectCast(Sender, PictureBox).Name
                    TankSelectStatus = 0
    
                    Dim AttackListIndex = GetSpriteIndex(AttackSprite)
                    Dim TargetListIndex = GetSpriteIndex(TargetSprite)
                    Dim hp As Integer = Sprites(TargetListIndex).HP - Sprites(AttackListIndex).AP
    
                    If hp <= 0 Then
                        MsgBox(Sprites(TargetListIndex).Name & " Murked by " & Sprites(AttackListIndex).Name)
                        Sprites(AttackListIndex).HP += Sprites(TargetListIndex).HP
    
                        Sprites(TargetListIndex).Pic.Dispose()
                        Sprites.Remove(Sprites(TargetListIndex))
                    Else
                        Sprites(TargetListIndex).HP = Sprites(TargetListIndex).HP - Sprites(AttackListIndex).AP
                        MsgBox("Still standing")
                    End If
                Else
                    AttackSprite = DirectCast(Sender, PictureBox).Name
                    TankToolbar.Location = New Point(Sprites(GetSpriteIndex(AttackSprite)).Pic.Right, Sprites(GetSpriteIndex(AttackSprite)).Pic.Bottom)
                    TankToolbar.Visible = True
                End If
            End If
    I was just looking the previous code to attacking sprites. could this help to do with attacking the sprites by range and fix the issue to do with its name? Its because this looks awfully well structured and the range reference will have to merge into the attack feature which makes sense so if the sprite is in range then It can be attacked. 


    WRA

    Tuesday, April 11, 2017 3:02 PM
  •    
      If e.Button = MouseButtons.Left Then
                If TankSelectStatus = 1 Then
                    TargetSprite = DirectCast(Sender, PictureBox).Name
                    TankSelectStatus = 0
    
                    Dim AttackListIndex = GetSpriteIndex(AttackSprite)
                    Dim TargetListIndex = GetSpriteIndex(TargetSprite)
                    Dim hp As Integer = Sprites(TargetListIndex).HP - Sprites(AttackListIndex).AP
    
                    If hp <= 0 Then
                        MsgBox(Sprites(TargetListIndex).Name & " Murked by " & Sprites(AttackListIndex).Name)
                        Sprites(AttackListIndex).HP += Sprites(TargetListIndex).HP
    
                        Sprites(TargetListIndex).Pic.Dispose()
                        Sprites.Remove(Sprites(TargetListIndex))
                    Else
                        Sprites(TargetListIndex).HP = Sprites(TargetListIndex).HP - Sprites(AttackListIndex).AP
                        MsgBox("Still standing")
                    End If
                Else
                    AttackSprite = DirectCast(Sender, PictureBox).Name
                    TankToolbar.Location = New Point(Sprites(GetSpriteIndex(AttackSprite)).Pic.Right, Sprites(GetSpriteIndex(AttackSprite)).Pic.Bottom)
                    TankToolbar.Visible = True
                End If
            End If
    I was just looking the previous code to attacking sprites. could this help to do with attacking the sprites by range and fix the issue to do with its name? Its because this looks awfully well structured and the range reference will have to merge into the attack feature which makes sense so if the sprite is in range then It can be attacked. 


    WRA


    Here is what I suggest. Make a new simple example similar to my last.

    Then you show us where in the new code you write it accomplishes the tasks for selecting the attacker as I laid out above. Here they are again:

         1 Upon mousedown identify which picturebox was clicked.

         2 If no attacker has been selected then save the current picturebox as the attacker.

         3 Draw a range circle around the attacker picturebox.

         4 Determine which of the pictureboxes is within the range circle,
         not including the attacker. These are the target pictureboxes.

         5 Save a list of the target pictureboxes.

         6 Draw circles around all the target pictureboxes in the list.

         7 Set a flag indicating the attacker has been selected.

         8 Await further instructions....................

    Now you take these tasks one at a time and write the code in between each one and show us the code that completes each of the tasks like my last example. Do not do task 3 until task 1 and 2 are written and working and debugged and understood.

    If you just come back with another copy of your game code that does not work I will not respond.

    Tuesday, April 11, 2017 3:24 PM
  • Alright I will follow the instructions accordingly without an issue. thanks again T. 

    I will take it one baby step at a time starting of with how will I highlight the first selected sprite. 


    WRA

    Tuesday, April 11, 2017 3:28 PM
  • Alright I will follow the instructions accordingly without an issue. thanks again T. 

    I will take it one baby step at a time starting of with how will I highlight the first selected sprite. 


    WRA

    You don't have to use a grid at this time. Just an example of what we are learning. How to use a class. What the difference is between what I am calling the string name of a picturebox and the control name and the name of the picturebox in the sprite class.

    Just use my last example and remove the mouse down code completely. Then you rewrite it.

    Remove the paint event for now.

    Just make a new example with 3 pictureboxes using any method you can figure sprite or not or whatever you come up with. All the example does is show how to select the attacker, save the attacker, determine what targets are within range of the attacker, make a list of the targets within range, and then draw the results.

    Just leave out the parts that use the grid rows and columns etc. We can ingnore that to get to just the selection of an attacker picturebox part for now.

    Tuesday, April 11, 2017 3:44 PM
  • @Tommy 

    In theory I think I have already done tasks one and two which is just a virtue of it being made in the past. 

    From the screenshot show I am able to select from Sprite to Sprite 


    WRA

    Tuesday, April 11, 2017 3:47 PM
  • Ohhh I see so leave out my example use the one you have just provided and understand what is being done in there first of all before moving to anything else. Thus those steps you mentioned arent for my sprite form but it is for the example you just gave today. 

    WRA

    Tuesday, April 11, 2017 3:49 PM
  • Ohhh I see so leave out my example use the one you have just provided and understand what is being done in there first of all before moving to anything else. Thus those steps you mentioned arent for my sprite form but it is for the example you just gave today. 

    WRA

    Just make a new example of the tasks. I don't care how. You write it. You debug it. You understand it. Feel free to ask questions.

    Its like learning fractions and math. You have to work many examples. Practice over and over. Some fractions are "nice" like 1/2, some are difficult like 1/3.

    Can we just 1/2 + 1/3 = ???

    No you have to

    1/2 = 3/6

    1/3 = 2/6

    3/6 + 5/6  =5/6

    They are all fractions. But they are each different. You have to equate them using similar things any way you can figure.

    Just like Sprite.name is not equal to picturebox.name.


    First you have to sprite picturebox name = sprite(i).pic.name.

    and picturebox name = picturebox1.name

    Now you can

       if sprite(i).pic.name = picturebox1.name then

    or you can

       if sprite(i).pic = picturebox1 then

    These two ifs do the same thing but use different ways to identify the objects.

    PS in one if we compare the object string names that we gave the objects. In the other we compare the class object to class object. Either way we compare apples to apples and oranges to oranges. Or feet to feet and meters to meters. Its still fruit. Still distance. Still pictureboxes.

    Tuesday, April 11, 2017 4:11 PM
  • Therefore for me to find the solution to my problem I need to understand your example in the most depth possible otherwise I'll be stuck in this issue longer than necessary

    WRA

    Tuesday, April 11, 2017 4:26 PM
  • Therefore for me to find the solution to my problem I need to understand your example in the most depth possible otherwise I'll be stuck in this issue longer than necessary

    WRA

    The example is not important. Understanding the bricks and wood and dirt and saw and hammer and nail are important. Build a birdhouse, build a sail boat. All the same. The thousandth time you hammer a nail will be much better than the first.

    Now stop talking and start doing Grasshopper.

    Tuesday, April 11, 2017 4:33 PM
  • Roger that

    WRA

    Tuesday, April 11, 2017 4:56 PM
  • I was typing up my program and during execution it corrupted man that just wound me up now and Im really planning to solve this issue by today with all the time I have thus far

    WRA

    Tuesday, April 11, 2017 7:50 PM
  • I took a look at it and yes realised that will act dysfunctionally, it will act on the bigger picture rather than each sprite alone.

    You are still talking in vague generalisations.  Provide the exact description of what is wrong with that code.  I am still not sure that you see the problem.

    Tuesday, April 11, 2017 9:14 PM
  • I dont see the problem Acamar and thats the truth there which is why Im going over the example Tommy has and following the instructions give to be able to understand properly what Im suppose to do. 

    Do you see what the problem is precisely Acamar? I have probably looked at the computer screen too long that I am not even seeing the actual problem that is somewhere in the code.


    WRA

    Tuesday, April 11, 2017 9:21 PM
  • I dont see the problem Acamar and thats the truth there which is why Im going over the example Tommy has and following the instructions give to be able to understand properly what Im suppose to do.

    This statement

        Dim distance As Double creates a variable 

    creates a variable named 'Distance' and assigns it a value of zero. But you never do anything with that variable. the next time it is used.

                    If distance < (Range * GridSize.Width) Then

    it will still be zero.  The test is pointless, and the code it controls will not execute.   It seems that you are assuming that Distance is somehow magically set to the correct value just by declaring it.  You need to create code to calculate the distance target according to the current state of play (shooter and target) and then update Distance with that calculation before attempting to use it in the test.

    The problem is not in the code: it is that you couldn't see such an elementary error.  It's the same issue as the previous thread when you changed my .Name property to .ToString and tried to claim it wasn't a change.  You can't just write arbitrary bits of code and expect them to work - you must do a detailed analysis of exactly what each bit of code does. 

    You have good examples, but if you can't look at your own code to see those elementary flaws, then I don't think you can look at the examples provided in order to see the underlying design ideas. The result is that you still don't have a good idea of what you are trying to do at the code level.  I have suggested that you write out what should happen in simple plain language, but you have ignored that and instead just produced more code, with basic errors in it.  That's why you aren't making progress.

    Don't just look at the examples.  Write a detailed comment line (or two, or three) above each line of code that exactly describes what that line of code is going to do.  Then make sure you understand why that code does what is described.  Then string those descriptions together so that you are sure they produce the result you want.

    Tuesday, April 11, 2017 10:19 PM
  • I like the way you are thinking. So you are telling me the piece of code I have shown on this thread you have actually taken it, tested it and it absolutely works for you?

    You raise a good point, examples are a good thing to look at perhaps I am acting over-dependent on them or along those lines.

    When it comes to writing in plain language what my code should do I find it "easier said than done" to operationalise the concepts. 

    I know I have learning difficulties and I don't want it to be a barrier for solving obstacles but I will try the very best I can and thank you all for bearing my misinterpretations. 

    So what I want my piece of code to do is as soon as I double click a sprite, it would show its range (a wide circle according to the size set) and it would show a side message to indicate which tanks are within its range and which ones are not. Then whichever tank is in range is vulnerable to attack. Once it is destroyed then it is no longer in range and gone so the tank that is attacking can only destroy other tanks which are in its presence and within the perimiter.


    WRA

    Tuesday, April 11, 2017 10:34 PM
  • I like the way you are thinking. So you are telling me the piece of code I have shown on this thread you have actually taken it, tested it and it absolutely works for you?

    No.  I haven't even bothered to test it because I can see by looking at it that it won't do anything.

    When it comes to writing in plain language what my code should do I find it "easier said than done" to operationalise the concepts.

    Yes - I can see that.  But until you can do it then you won't make progress.  Perhaps you need to do it differently then I have described - perhaps a diagram would be better than words.  But you need to have, yourself, in your own head, a description of what you need to do at the level of each individual line of code (and that one line of code needs to be as simple as you can make it!).  It might be a good idea to take a break from this example, take a simpler task, and use it as an exercise in developing code, one line at a time, from scratch, to accomplish one simple task, by spelling out what that line of code needs to do before you start thinking about what it's going to look like.  You can't operationalise the concept by writing the code and then trying to work out why it doesn't work - you must start from a clear understanding of what you need that code to do.

    So what I want my piece of code to do is as soon as I double click a sprite

    You can't double-click a sprite. You can double-click a picturebox.  So you are writing code in the picture box double click event.

    it would show its range

    So you need to calculate the effective position of the picture box (which will be the centre of the control). You will calculate that from the control's position and size.  Note that this assumes that distance, range etc are going to be in screen pixels - some time ago you decided not to work in grid squares (which would actually be simpler).  You could change the whole program to work in grid squares instead of pixels without much work.

    it would show its range (a wide circle according to the size set)

    So you need to draw a circle on the form.  But stop right there.  What is 'size set?  For now, write it in as a constant (with a big comment that it is a TBD).

    it would show a side message to indicate which tanks are within its range

    Then you need a routine to find all the tanks that have a distance from your calculated position that is less than the 'size set' (which for now you have assumed as a constant). A tank is a (certain type of?) sprite, so you need to look through your list of sprites, find the location of each one, find out the distance from your picturebox location to that sprite's location, and compare that distance to the 'size set'.

    That's enough to be going on with, but even at that level it is not detailed enough.  You need to expand it into more and more detail until you get close enough to one line of code per comment, then try to create the code from the comments.

    Then write up the code and comments, run it in the debugger, and at each line stop the debugger and make sure the code has done what its description said it was going to do.

    Tuesday, April 11, 2017 11:55 PM
  • I see where you are getting at, thanks for the in depth Acamar. I realise If I want the range feature to work I am suppose to be using the previous code that I have made such as the AttackSprite and TargetSprite and several of the other functions.

    Sprite is a picturebox and I should keep that In mind. Im gonna take a look at this code one more time If I can solve it then I might move to another phase and try again this issue once the next few problems are solved.


    WRA

    Wednesday, April 12, 2017 10:32 AM
  • I just realise this the the two functions and sub Tommy uses in his example isn't going to work in mine because his is assigned as pictureboxes compared to mine which is a sprite (practically its a picturebox) so the function and the sub procedure is going to be of no use to me what I need to do is follow what Acamar is saying write code, comment it and debug it.

    WRA

    Wednesday, April 12, 2017 10:40 AM
  • I found the first step of mistake when I was stepping through the code, range is assigned as 0 therefore I need to assign that to the given range number.

    WRA

    Wednesday, April 12, 2017 11:20 AM
  •  If e.Button = MouseButtons.Left Then
                TargetSprite = DirectCast(Sender, PictureBox).Name
                'Declare variables
                Dim Scope As Integer = Range
                Dim AttackListIndex = GetSpriteIndex(AttackSprite)
                Dim TargetListIndex = GetSpriteIndex(TargetSprite)
    
                'Checks the sprite range 
                If Scope = 4 Then
                    'Identifies the given location of the sprite - the co-ordinates from colrow in load form
    
                    'Indicates the names of the following sprites that are in the given perimiter
                    MsgBox(Sprites(TargetListIndex).Name & " In danger " & Sprites(AttackListIndex).Name)
                Else
                    'Indicates the names of the following sprites that are still in its range and exclude the ones that aren't
                    MsgBox(Sprites(TargetListIndex).Name & " Safe " & Sprites(AttackListIndex).Name)
    
                End If
            End If

    Quick question Tommy or Acamar. Never mind about the Piece of code above because it shows the error attacklistindex is out of range and through debugging I saw attacklistindex became -1 as soon as we reachec the if statement 


    WRA

    Wednesday, April 12, 2017 11:42 AM
  • I am suggesting (I think someone here recommend this) to create a function that updates the column and row movement of each sprite whether attacking or is the target.

    WRA

    Wednesday, April 12, 2017 11:43 AM
  • Sprite is a picturebox and I should keep that In mind.

    No.  A sprite is not a picturebox.  A sprite is associated with a picture box (and vice versa) but they are two different things. One is an instance of a class that you have defined.  The other is a control that you created on the form.

    Wednesday, April 12, 2017 12:11 PM
  • Oh yes I explain it along the wrong lines darn it. Ill remember sprite is an associate of the picturebox.

    WRA

    Wednesday, April 12, 2017 12:40 PM
  • Wali,

    "shows the error attacklistindex is out of range and through debugging I saw attacklistindex became -1 as soon as we reachec the if statement "

    Describe what this line of code does?

       Dim TargetListIndex = GetSpriteIndex(TargetSprite)

    In your code what data type of variable is TargetSprite?

    In your code what type of variable is TargetListIndex ?

    And what value will the function return for the possible values of targetsprite?

    You might as well stop working on your big project until you understand these basic concepts about programming. What is the difference between variable types, a class object, a control, and a list. And how to use each.

    You should just go back and learn these basic concepts with simple examples for a month or two. You will never get your game to work until you learn the basics first.

    Wednesday, April 12, 2017 12:41 PM
  • thats the time I dont have tommy I wish I did have months but that is what I don't have currently so I'm gonna have to use the time now to actually solve the problem. 

    WRA

    Wednesday, April 12, 2017 12:49 PM
  • Dim TargetListIndex = GetSpriteIndex(TargetSprite)

    Ohhh I can see what you are trying to get me to think about. I have assigned targetlistindex, something which is an integer which is assigned to the function of getspriteindex which converts its function "thename" as an integer while getsprite is integer.

    The targetsprite is referrenced as string. 

    So in the code to do with the attack sprite and destroys it. it will call it according to its name so in the msgbox it will tell us what is the name of the tank that successfully destroys and which tank is the one that has been destroyed.


    WRA

    Wednesday, April 12, 2017 12:53 PM
  • Dim TargetListIndex = GetSpriteIndex(TargetSprite)

    Ohhh I can see what you are trying to get me to think about. I have assigned targetlistindex, something which is an integer which is assigned to the function of getspriteindex which converts its function "thename" as an integer while getsprite is integer.

    The targetsprite is referrenced as string. 

    So in the code to do with the attack sprite and destroys it. it will call it according to its name so in the msgbox it will tell us what is the name of the tank that successfully destroys and which tank is the one that has been destroyed.


    WRA

    Maybe. Since you did not show the code for GetSpriteIndex we cant be sure.

    However since I wrote that function I know what I meant it to do at the time.

    You wrote this line of code:

         Dim TargetListIndex = GetSpriteIndex(TargetSprite)

    yet you did not declare TargetListIndex as anything so the system assumes its an object. But it is not. Are you using Option Strict On? Then you should get an error there I think.

    What did you want that line to do?

    What is the target list and why are you using the function? What values will the function return for each value of TargetSprite?

    Assuming TargetSprite is the name of the sprite then what will the function return if TargetSprite is "Pic 1" or what if the value is "nonsense" or whatabout   ""    (empty string) ?

    What if TargetSprite is not in the list? What value does the function return?

    Why dont you make a simple one form example that just calls this function and learn how the function works. Just make a list or lists the same way. Like the range example I last posted on this thread. Show me the answers to these questions in the example. It just a learning example.

    It does not matter how much time you have. You cant do it if you dont know how each part works. You are trying to build a rocket but you dont know enough to build a birdhouse, yet.

    Just trying to help. Don't take it the wrong way.   :)

    Wednesday, April 12, 2017 1:27 PM
  • Its cool man I get what you are trying to say here I'm not taking it to heart what you are saying its the truth, you are trying your very best to help me out here and I'm appreciated for that otherwise I wouldn't be this far if it wasn't thanks for the people on MSDN (including you) 

    PS yes I have been using OptionStrict On (very helpful) it points out my errors but funny enough for the variable there it isn't pointing out that something is wrong there. 

    I will take a good think about what is going wrong here meanwhile I'm gonna work on some of the easy parts to my main program such as a manual guide, load the position of the buildings for when the game begins etc. 

    I think I need the easy stuff coming to my mind because its been a days and week streak of complexity coming to me. 

    But no matter what I am still going to solve this range problem. 


    WRA

    Wednesday, April 12, 2017 1:46 PM
  • @Tommy

    I think this method is just too complex for me, It is not that I am not trying to get my head around this method but it is that I can't understand the advanced work around it is all. 

    My friend recommended I could do this because the one at the moment I am trying to solve is too complex for me. this game is called "enchanted Arm"

    So the feature I want to take from this is still the feature of highlighting the selected sprite/tank but instead of showing a wide circle of what it can attack I think this approach from the screenshot shows the positions and highlight the squares that it can attack to.


    WRA

    Wednesday, April 12, 2017 2:46 PM
  • Well I am not saying give up. I just mean you need to do some basic drilling of the syntax and etc and then you will be able to continue your game. One can also make things too complicated when you don't understand how simple it is.

    Yes take breaks with doing other fun things.

    So in the mean time this morning I did this example. It selects the two stages attacker, target. In addition if you rmb click the picturebox it shows info for that picturebox.

    This example is basically the same as the others but its slightly different and I added the last step to select the target.

    I would like you to finish the GetNameDataFromPicturebox function so it shows the name of the picturebox that was clicked. And study the rest. Ask questions.

    Also do what Acamar suggests.

    Option Strict On
    
    Public Class Form5
        Private MyPicList As New List(Of PictureBox)
        Private MyTargetList As New List(Of PictureBox)
        Private AttackerPic As PictureBox
        Private TargetPic As PictureBox
        Private InstructionMsg As String = "LMB Select Attacker" & vbLf & "RMB for Info"
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
    
            For i = 0 To 4
                Dim thisPic As New PictureBox
                thisPic.Name = "My Pic " & i.ToString
                thisPic.BackColor = Color.FromArgb(255, i * 50, 255 - (i * 50), 100)
                thisPic.Location = New Point((i * 50) + 20, (i * 50) + 20)
                thisPic.Size = New Size(50, 50)
                AddHandler thisPic.MouseDown, AddressOf Pics_MouseDown
                Controls.Add(thisPic)
                MyPicList.Add(thisPic)
            Next
        End Sub
    
        Private Sub Pics_MouseDown(sender As Object, e As MouseEventArgs)
            If e.Button = MouseButtons.Left Then
                If AttackerPic Is Nothing Then
    
                    'save the picturebox that was clicked as attacker
                    AttackerPic = DirectCast(sender, PictureBox)
    
                    'make the targets in range list
                    MyTargetList.Clear()
    
                    For i = 0 To MyPicList.Count - 1
                        If MyPicList(i).Location.X < 150 And AttackerPic IsNot MyPicList(i) Then
                            'these pics are on the left side of the form
                            MyTargetList.Add(MyPicList(i))
                        End If
                    Next
    
                    InstructionMsg = "Select the Target"
                Else
                    'select the target
                    Dim thisPic As PictureBox = DirectCast(sender, PictureBox)
                    For Each pic In MyTargetList
                        If pic Is thisPic Then
                            TargetPic = thisPic
                            InstructionMsg = "Target Selected"
                        End If
                    Next
    
                    If TargetPic Is Nothing Then InstructionMsg = "Target Out of range"
    
                End If
    
            ElseIf e.Button = MouseButtons.Right Then
    
                'list the picturebox that was clicked name and target list index
                Dim thisPic = DirectCast(sender, PictureBox)
                Dim NameData As String = GetNameDataFromPicturebox(thisPic)
                Dim targetIndex As Integer = GetTargetListIndexFromPicturebox(thisPic)
                InstructionMsg = NameData & vbLf & "Target List Index: " & targetIndex.ToString
    
            End If
    
            Refresh()
        End Sub
    
        Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim rect As Rectangle
    
            If AttackerPic IsNot Nothing Then
                rect = AttackerPic.Bounds
                rect.Inflate(10, 10)
                e.Graphics.FillEllipse(Brushes.Yellow, rect)
            End If
    
            For Each pic In MyTargetList
                rect = pic.Bounds
                rect.Inflate(10, 10)
                e.Graphics.DrawRectangle(New Pen(Color.Blue, 2), rect)
            Next
    
            If TargetPic IsNot Nothing Then
                rect = TargetPic.Bounds
                rect.Inflate(10, 10)
                e.Graphics.FillEllipse(Brushes.Red, rect)
            End If
    
            e.Graphics.DrawString(InstructionMsg, New Font("tacoma", 12, FontStyle.Bold), Brushes.Black, 100, 20)
        End Sub
    
        Private Function GetNameDataFromPicturebox(thisPic As PictureBox) As String
            GetNameDataFromPicturebox = "Name"
        End Function
    
        Private Function GetTargetListIndexFromPicturebox(thisPic As PictureBox) As Integer
            GetTargetListIndexFromPicturebox = -1
    
            For i = 0 To MyTargetList.Count - 1
                If MyTargetList(i) Is thisPic Then
                    GetTargetListIndexFromPicturebox = i
                    Exit For
                End If
            Next
    
        End Function
    
    End Class


    • Edited by tommytwotrain Wednesday, April 12, 2017 3:47 PM new animation
    Wednesday, April 12, 2017 3:20 PM
  • Sounds like a good idea Tommy before I try again this complex part Im just gonna finish some of the simple parts to my project since they are relatively easy and straightforward such as designing the game map. 

    WRA

    Wednesday, April 12, 2017 3:44 PM
  •  If e.Button = MouseButtons.Left Then
                TargetSprite = DirectCast(Sender, PictureBox).Name
                'Declare variables
                Dim Scope As Integer = Range
                Dim AttackListIndex = GetSpriteIndex(AttackSprite)
                Dim TargetListIndex = GetSpriteIndex(TargetSprite)

    You have five lines of code and one comment.  And that comment doesn't really say anything.  You should have five lines of code and at least five comprehensive comments.  Then you have something to check each line of code against to confirm that it is doing what you think it is doing.

    Wednesday, April 12, 2017 9:21 PM
  • My friend recommended I could do this because the one at the moment I am trying to solve is too complex for me. this game is called "enchanted Arm"

    I can't tell what your image is demonstrating, but changing the game play so that each tank can only attack a sprite on an adjacent grid position is a worthwhile simplification.  You can always change that later.   Simplest of all would be a rule that the tank can only shoot horizontally and vertically.

    But that problem is still further down the track - you don't yet have correct code to discover which sprite the user has selected. You need to focus on the immediate task.

    Wednesday, April 12, 2017 9:31 PM
  • Yes indeed, my coursework has a lot of complexity so I want to make this part just simplistic and nothing too difficult, I will just enable the tanks and turrets to attack horizontally only and nothing more since there is already loads of complexity throughout the program I should focus on making things simple

    WRA

    Thursday, April 13, 2017 9:11 AM
  • Tommy when I select the target sprite it stays frozen there. Is that what this program does it doesnt let me select any of the other sprites such as the ones out of range.

    WRA

    Thursday, April 13, 2017 11:44 AM
  • @Tommy 

    Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
            'Left mouse button
            If e.Button = MouseButtons.Left Then
                If AttackSprite Is Nothing Then
                    'save the sprite that was clicked as attacker
                    TargetSprite = DirectCast(Sender, PictureBox).Name
    
                    'make the targets in range list
                    Sprites.Clear()
    
                    'Select the attacking sprite in preparation who to attack 
                    For i = 0 To Sprites.Count - 1
                        If Sprites(i).ColRow.X < 150 And TargetSprite IsNot Sprites(i) Then
                            'these sprites are on the left side of the form
                            Sprites.Add(Sprites(i))
                        End If
                    Next
                    MsgBox("Select the Target")
               End If
            End If
        End Sub
    This piece of code shows the message successfully that the attack sprite has been selected, Is there a way to paint a highlight in the form paint feature to highlight the attacking sprite just to make sure that it is working. 


    WRA

    Thursday, April 13, 2017 12:27 PM
  • @Tommy

    Dim rect As Rectangle
            If AttackSprite IsNot Nothing Then

                rect.Inflate(10, 10)
                e.Graphics.FillEllipse(Brushes.Blue, rect)
            End If

    this piece of code is in the form_paint 

    As soon as i select any of the sprites it shows a little blue square on the top left of the screen, how do I get the code to do it to select the attacking sprite


    WRA

    Thursday, April 13, 2017 12:39 PM
  • Tommy when I select the target sprite it stays frozen there. Is that what this program does it doesnt let me select any of the other sprites such as the ones out of range.

    WRA

    Yes that is what the last example I posted with 5 pictureboxes does. The player selects the attacker, the targets within range are highlighted, then the player selects the target tank from those in range. If the player selects a tank out of range then it says out of range and awaits selection of a tank in range.

    If you want to do it another way then you will need to change it.

    Thursday, April 13, 2017 12:41 PM
  • I see so how would I using your example get the code to select each sprite such as I select the first square then the second square and so on. 

    WRA

    Thursday, April 13, 2017 12:42 PM
  • @Tommy 

    Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
            'Left mouse button
            If e.Button = MouseButtons.Left Then
                If AttackSprite Is Nothing Then
                    'save the sprite that was clicked as attacker
                    TargetSprite = DirectCast(Sender, PictureBox).Name
    
                    'make the targets in range list
                    Sprites.Clear()
    
                    'Select the attacking sprite in preparation who to attack 
                    For i = 0 To Sprites.Count - 1
                        If Sprites(i).ColRow.X < 150 And TargetSprite IsNot Sprites(i) Then
                            'these sprites are on the left side of the form
                            Sprites.Add(Sprites(i))
                        End If
                    Next
                    MsgBox("Select the Target")
               End If
            End If
        End Sub
    This piece of code shows the message successfully that the attack sprite has been selected, Is there a way to paint a highlight in the form paint feature to highlight the attacking sprite just to make sure that it is working. 


    WRA

    You can do it like I show in my last example. When the attacker has been identified then draw it in the paint event. To fire the paint event from mouse down call the picturebox.refresh method.

    There are other ways if this does not work for you for some reason.

    These are just examples. You will need to modify them for your exact needs and desires.

    Thursday, April 13, 2017 12:44 PM
  • What code in particular should I look at to make the doubleclick used more than once? does the answer lies somewhere in my example to do with the attacking of the sprites?

    WRA

    Thursday, April 13, 2017 12:46 PM
  • You know I figured out the part to select the sprite my code here I want it to now target the actual sprite so I know and can confirm that it works. 

    Else
                    'select the target                
                    TargetSprite = DirectCast(Sender, PictureBox).Name
    
                    For Each pic In Sprites
                        If TargetSprite Is TargetSprite Then
                            MsgBox("Target Selected")
                        End If
                    Next
                    Refresh()
                End If
    This is the else part for the statement but I think there is something wrong in someway possible :(


    WRA

    Thursday, April 13, 2017 12:51 PM
  • You know I figured out the part to select the sprite my code here I want it to now target the actual sprite so I know and can confirm that it works. 

    Else
                    'select the target                
                    TargetSprite = DirectCast(Sender, PictureBox).Name
    
                    For Each pic In Sprites
                        If TargetSprite Is TargetSprite Then
                            MsgBox("Target Selected")
                        End If
                    Next
                    Refresh()
                End If
    This is the else part for the statement but I think there is something wrong in someway possible :(


    WRA

    Whomever wrote that code must not have any idea of what they are doing and needs to practice more simple examples.

    No offense.


    PS I hope it was not me. :)
    Thursday, April 13, 2017 1:03 PM
  • LOOOL dont worry it weren't you. You have better expertise than this person. 

    PS that isn't my working version 

    Anyways how would I actually change this to make it work to show the msgbox that the target sprite is selected.


    WRA

    Thursday, April 13, 2017 1:11 PM
  • LOOOL dont worry it weren't you. You have better expertise than this person. 

    PS that isn't my working version 

    Anyways how would I actually change this to make it work to show the msgbox that the target sprite is selected.


    WRA

    Use the correct objects in the code. You have to practice it. It takes time. Sorry.

    Here is my latest version. Just to brag. And shown you it can be done. It just takes practice. I have practiced for over 30 years.

    Thursday, April 13, 2017 1:16 PM
  • @Tommy 

    you are evil :( Im just a 1st year programmer and boy that actually looks soooooooo beautiful Tommy loada props to the game layout there I am just impressed. 


    WRA

    Thursday, April 13, 2017 1:21 PM
  • @Tommy 

    you are evil :( Im just a 1st year programmer and boy that actually looks soooooooo beautiful Tommy loada props to the game layout there I am just impressed. 


    WRA


    Well thanks, I guess. Its easy for me. I was blessed with two halfs of my brain that work. The engineer side and the artist side.

    In life we should do the things we enjoy and are good at. I love designing, solving problems, drawing pretty little pictures. Whalaa! I found my calling. Its not for everyone though.

    Now spelling an history? Naaah.

    This has been fun for me and I learned some things. I may add this tile drawing to my number one app.

    Thursday, April 13, 2017 1:32 PM
  • sounds like a good idea, you should go for it.

    This solution is getting me closer to the solution for the tank range, I surely hope this can be solved because that will be a major celebration to be cheering about.



    WRA

    Thursday, April 13, 2017 1:36 PM
  • @Tommy

    I see this part here 

                rect = TargetPic.Bounds

    obviously if i tried to do that with targetsprite it will say the two aren't members therefore how would I assign it for the selected sprite instead. 


    WRA

    Thursday, April 13, 2017 1:37 PM
  • @Tommy

    I see this part here 

                rect = TargetPic.Bounds

    obviously if i tried to do that with targetsprite it will say the two aren't members therefore how would I assign it for the selected sprite instead. 


    WRA

    You just need the reference to the picturebox object. There are many ways to do that. Here is one.

       mysprite(i).pic.bounds

    If you have made a picturebox variable then use that.

      ie Dim Target as picturebox = Directcast(sender, picturebox)

    then you can use Target.bounds

    Both cases we use the reference to the picturebox class object. Just differenct ways.

    This has already been explained in this thread a bunch of ways.

    Thursday, April 13, 2017 2:26 PM
  • True I could but my teacher was just telling me I should refer to my class along the size of it, width etc to figure out its location. Could that be the lines of solving the problem. 

    WRA

    Thursday, April 13, 2017 2:42 PM
  • I am not sure what you mean or what your teacher meant.

    If you have a reference to the picturebox class object that is all you need. The class contains the location and size of the picturebox. You can make the rectangle like this

      Dim rect as new Rectangle(picturebox1.left, picturebox1.top, picturebox1.width, picturebox1. height)

    or you can

       Dim rect as rectangle = Picturebox1.bounds

    there are many ways. Its up to you to decide. As long as it works.

    PS The solution to the problem is to understand the problem. The problem is you don't understand every little detail. Not really a problem. Just something that has to be done before the project is complete.

    Thursday, April 13, 2017 3:00 PM
  • Fair enough.

    So the location of the sprite position is indicated by the class Sprite "public colrow as point" 

    in the load stage of the form the position for one of the sprites is "Sprites(1).colrow(2,3)

    The reference to my picturebox I can see that in my Sprite class I have declared the "Pic as picturebox" 

    Ill go for the way which will answer my first part of the puzzle which is to highlight around the selected sprite to attack. Before anything else this should prior to function and I am closer than I was for when I got stuck with the last example for a week. 


    WRA

    Thursday, April 13, 2017 5:05 PM
  • Wali,

    Look at how the flow goes in my last 5 picturebox example. It has everything you need for this task. If you use double click event for yours instead of mousedown its still the same as far as this task. When the event occurs do this... when the flag is true do that...

    "So the location of the sprite position is indicated by the class Sprite "public colrow as point" "


    There is no sprite position. The sprite is nothing. Its a list of data. It is a piece of paper with some writing on it not a pictureframe.

    This is the big point you get disconnected on. Its why the name of the sprite and the name of the picturebox are different. Pic1 verses Sprite(i).pic.

    We could have left the sprite out completely. In my first example here I did.

    We only use the sprite for programming convienence. And in some things we need the classes or it gets too complicated. They help do what computers are good at.

    One data item is ".pic" in your sprite class. That is just a reference to the real picturebox control class object.

    The position is in the picturebox control. All our sprite does is tell us which picturebox it is.

    The position is a property of the picturebox refered to by our class.

      Picturebox1.location.X

    or

      MySprite(thisSpriteIndexInTheList).pic.location.X

    are the same thing.

    Both

       MySprite(thisSpriteIndexInTheList).pic

    and

       Picturebox1

    are the same thing. A number that points to the location in memory of where the code for that particular picturebox begins. They are the same thing.

    You have a name Wali, you have a phonenumber. You can be reached using either. But, they are just bits of data in your pocket. You are you the human class object named Wali. Name and phone number are some of your properties and methods.

    Row and Col are the same thing as X, Y just different by the scale ratio. Just like Meters and Centimeters. Meters = scale ration (centimeters)  where scaleratio =100. How big is your tile? Mine is 50 pixels. And my tile is 1 column. So the scale ratio is 50.

    Thursday, April 13, 2017 5:29 PM
  • I to be honest didn't see any benefit using the scale ratio so I got rid of it.

    If you don't mind send me a link to the relevant examples that I need to be looking at. I already know about the one on here about the target sprite and highlighting them thats one. 


    WRA

    Thursday, April 13, 2017 6:04 PM
  • Afternoon Tommy

    I was so onto something here. I can get paint splodge to display but it is in the top left hand corner of the windows form.

        Private Sub Pics_DoubleClick(Sender As Object, e As MouseEventArgs)
    If AttackSprite Is Nothing Then
                'save the sprite that was clicked as attacker
                AttackSprite = DirectCast(Sender, PictureBox).Name
    
                'make the targets in range list
                Sprites.Clear()
    
                'Select the attacking sprite in preparation who to attack 
                For i = 0 To Sprites.Count - 1
                    If Sprites(i).ColRow.X < 150 And TargetSprite IsNot Sprites(i) Then
                        'these sprites are on the left side of the form
                        Sprites.Add(Sprites(i))
                    End If
                Next
                MsgBox("Select the Target")
                Refresh()
            End If
        End Sub

    This is as soon as the sprite has been selected and that indicates working. 

    the paint section for the windows form is here:

        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    'Paints but not in the positon intended
            Dim rect As Rectangle
            rect.Inflate(10, 10)
            e.Graphics.FillEllipse(Brushes.Blue, rect)
        End Sub


    WRA

    Friday, April 14, 2017 2:09 PM
  • Perhaps you need to define the size and location of the rectangle, in pixels.

    All you did was declare an empty variable.

    Friday, April 14, 2017 2:31 PM
  • Should this be done in the load section of the windows form? 

    plus I was just looking at these two functions and thought which one will be of most help to reach the answer?

    Private Function GetGridRectFromColRow(thisColRow As Point) As Rectangle
            'make the rectangle for the grid square at col row
            GetGridRectFromColRow = New Rectangle(GridRect.X + (thisColRow.X * GridSize.Width),
                            GridRect.Y + (thisColRow.Y * GridSize.Height), GridSize.Width, GridSize.Height)
        End Function
    
        Private Function GetColRowFromPoint(thisPt As Point) As Point
            'convert pixel location to grid cols and rows
            GetColRowFromPoint = New Point(CInt(Math.Floor((thisPt.X - GridRect.X) / GridSize.Width)),
                                CInt(Math.Floor((thisPt.Y - GridRect.Y) / GridSize.Height)))
    
            If GetColRowFromPoint.X < 0 Then GetColRowFromPoint.X = 0
            If GetColRowFromPoint.X > GridColRows.X - 1 Then GetColRowFromPoint.X = GridColRows.X - 1
            If GetColRowFromPoint.Y < 0 Then GetColRowFromPoint.Y = 0
            If GetColRowFromPoint.Y > GridColRows.Y - 1 Then GetColRowFromPoint.Y = GridColRows.Y - 1
        End Function


    WRA

    Friday, April 14, 2017 2:38 PM
  • Should this be done in the load section of the windows form? 

    plus I was just looking at these two functions and thought which one will be of most help to reach the answer?

    Private Function GetGridRectFromColRow(thisColRow As Point) As Rectangle
            'make the rectangle for the grid square at col row
            GetGridRectFromColRow = New Rectangle(GridRect.X + (thisColRow.X * GridSize.Width),
                            GridRect.Y + (thisColRow.Y * GridSize.Height), GridSize.Width, GridSize.Height)
        End Function
    
        Private Function GetColRowFromPoint(thisPt As Point) As Point
            'convert pixel location to grid cols and rows
            GetColRowFromPoint = New Point(CInt(Math.Floor((thisPt.X - GridRect.X) / GridSize.Width)),
                                CInt(Math.Floor((thisPt.Y - GridRect.Y) / GridSize.Height)))
    
            If GetColRowFromPoint.X < 0 Then GetColRowFromPoint.X = 0
            If GetColRowFromPoint.X > GridColRows.X - 1 Then GetColRowFromPoint.X = GridColRows.X - 1
            If GetColRowFromPoint.Y < 0 Then GetColRowFromPoint.Y = 0
            If GetColRowFromPoint.Y > GridColRows.Y - 1 Then GetColRowFromPoint.Y = GridColRows.Y - 1
        End Function


    WRA

    You define it when required for the project. Just like x = 3.

    I don't know if you will find that useful or not. You certainly cant copy it into your project and have it work without modifications.

    Maybe you need a scaleratio to convert from rows and columns to pixels that you draw with and visa versa?

    Or maybe you can do as I did in my examples.

    There are many ways to do something.

    Perhaps this code will work for you?

      Dim GarbageOut As String = "Garbage In"

    Friday, April 14, 2017 3:03 PM
  • Ohh so use the defined position of each of the sprites from the start of the prorgram such as in the load form I set the first sprites.colrow (2,4) 

    I'll try the scale ratio out, it maybe of some use to me perhaps I just don't realise it yet. 


    WRA

    Friday, April 14, 2017 3:12 PM
  • I think I finally understand and realise it is just impossible to assign the bounds to a sprite but I can assign bounds to a picturebox. 

    Therefore I need to declare "aimpic" as picturebox then need to link that to my sprites which will then mean I am able to assign the pic.bounds feature so it is able to highlight the selected tank. Then I can move onto highlighting the spaces it can attack around. 


    WRA

    Saturday, April 15, 2017 11:33 AM
  • I will understand your most recent example, best way I find it typing it out, it is the long way but I find pieces of code that could help in my example to reach my answer. 

    WRA

    Saturday, April 15, 2017 11:34 AM
  • I think I finally understand and realise it is just impossible to assign the bounds to a sprite but I can assign bounds to a picturebox.

    The sprite is your own invention.  It can do anything you want it to do, including having a boundary (if that's what you are referring to) and having limitations on how far it can shoot (which I think is where this thread started).   That's part of the reason for using your own object - you decide what properties and methods it needs in order to be useful as an object in the application.

    Adding additional features to predefined objects is a bit more involved, but that is certainly possible also.  Objects are tools that you create or modify for the purposes that you need.

    Saturday, April 15, 2017 12:07 PM
  • I see so to solve the problem I need to define new variables in the sprite class to be able to operate with the pic bounds process.

    WRA

    Saturday, April 15, 2017 12:50 PM
  • Were getting somewhere T. 

    Progress after a long while of 2 weeks. 

    We succeeded it into selecting the sprite, highlights it and indicates that the target is out of range. 


    WRA

    Saturday, April 15, 2017 2:43 PM
  • But one problem Tommy, it won't allow me to select from sprite to sprite such as select another sprite other than the one I just selected as seen with the screenshots. 

    How would I just using your example adjust it to select from sprite to sprite?


    WRA

    Saturday, April 15, 2017 2:45 PM
  • The next part is for the selected sprite to process range section which is to highlight squares which it can attack and if any of the sprites are in any of those squares can be attacked. 

    I will use your example which has been the best of help. (most recent example). 


    WRA

    Saturday, April 15, 2017 2:51 PM
  • But one problem Tommy, it won't allow me to select from sprite to sprite such as select another sprite other than the one I just selected as seen with the screenshots. 

    How would I just using your example adjust it to select from sprite to sprite?


    WRA


    Well thats looking better Wali.

    I am not sure what problem you are having. I dont have the code.

    My example allows you to keep picking the target as I recall. If an invalid target is selected the example gives a message and awaits selection of a valid target.

    After the attack sprite is set the example only selects target unless you add code to reset. ie AttackPicturebox = nothing. Otherwise you can keep selecting targets at that stage.

    So whatever you use to determine what stage you are at, ie in my example if AttackPicturebox is not nothing then choose target, I use AttackPicturebox as my flag, just reset the flag to step 1 or whatever.

    You may need to add a cancel button. Or use rmb for cancel etc. Or if target not in range then reset target select or whatever.

    • Marked as answer by Waliur Rahman Saturday, April 15, 2017 3:02 PM
    Saturday, April 15, 2017 2:54 PM
  • @Tommy, Acamar and others

    I'll open a new thread for this so this can continue on there sound good?


    WRA

    Sounds like this thread is moving on to another thread, if any of the replies withing this thread have been seen as helpful I would suggest voting on those replies. I for one noted two which I voted for in regards to tommytwotrain's assistance.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 15, 2017 2:58 PM
    Moderator
  • Its been a while Karen how are you? 

    Good idea I will forward this thread and send a link to the next part of it. 


    WRA

    Saturday, April 15, 2017 3:01 PM
  • Sounds like an excellent idea T and thanks everyone in this thread for the help. This was one of the most difficult and longest process stage into solving a problem but it has proven valuable experience to me and im sure experience for you all. 

    WRA

    Saturday, April 15, 2017 3:02 PM
  • https://social.msdn.microsoft.com/Forums/en-US/ebd5af3a-6ce4-4cf1-b7e9-6a6f02eeb3e7/continuation-of-the-last-thread-on-range-target-mode-this-time?forum=vbgeneral

    Here is the link to the next thread 


    WRA

    Saturday, April 15, 2017 3:08 PM