none
Why cant the sprite remove itself when vb indicates through an if statement that it is destroyed?

    Question

  • Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Sprites(1).HP = Sprites(1).HP - Sprites(2).AP
            Label1.Text = Sprites(1).HP.ToString()
            Label2.Text = Sprites(2).AP.ToString()
    
            If Sprites(1).HP <= 0 Then
                MsgBox("Murked")
                'remove sprite from list
                Sprites.Remove(Sprites(1))
            Else
                MsgBox("Still standing")
            End If
        End Sub

    1) It shows a major error when I try to remove the tanks from play. Here is a screenshot to show the issue.

    I dont really know if this would be of any use to you but worth a shot.

    2) Would I need to create a boolean statement to actually determine if the tank is destroyed as seen with your project because from my one when I keep pressing the button the tank hp goes onto negative numbers. 


    WRA

    Thursday, March 23, 2017 9:05 PM

Answers

  • Ohhhhhh I see so basically rather than trying to actually remove this tank I can just label it as dead so when it is long gone any other tank can practically walk over it. 

    In other words, the solution I'm looking for is to remove the sprite from play with an indication to show that it is down for the count? 

    Also the influences of the variable so It needs to be manipulated for Y to attack other than just X so if there are other sprites at present then it should be able to attack Z or B or C and so on. 

    In a nutshell, I need to revamp my if statement to include features to indicate if X isn't available to attack then it attacks B and so on.


    WRA


    Wliur, 

    You are not far from understanding OOP with your reply. 

    Know that OOP is based on New construction than Reusing. Reusing was in past cheap, but likewise real life, it has become expensive. Depending from where you life is getting somebody to repair your coffee machine more expensive than to buy a new coffee machine. That is also with OOP and with computing it is not important where you life. 

    If you buy (construct) a new coffee machine, you start using that. You put the old coffee machine at the Garbage (which is automatically done if you construct a new one). When that old machine is collected is not important to you. The Garbage Collector takes care of that. 

    You go on with the new coffee machine and use it as if it was the old by setting its properties in the way you wish. 


    Success
    Cor


    Friday, March 24, 2017 9:35 PM
  • Ill get back to square one the way I actually originated these indexes then process from there.

    You need to fix this code first.

    You do this:

            For i = 1 To 3
                Dim spt As New Sprite
                ...
                Sprites.Add(spt)
            Next

    Then you do this:

            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
            Sprites(3).HP = 10
            Sprites(3).AP = 5
    That first bit of code creates Sprites(0), Sprites(1) and Sprites(2) in the list.  Sprites(3) does not exist.  That code will fail. I think everyone assumed this had already been fixed when it was first pointed out.  If your code ever executes beyond that point there is something very magical going on.
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 3:08 PM
    • Unmarked as answer by Waliur Rahman Saturday, March 25, 2017 3:56 PM
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 4:59 PM
    Friday, March 24, 2017 11:53 PM
  • I see. Quick question the code that Im using now to indicate a tank is destroyed is that practically the same code for when I will change it to selecting sprites with a mouse where I select a sprite and which sprite it should attack

    WRA


    Learn by example you said? Have you learned anything? :)

    So you can either remove the Sprites from the list when destroyed or you can flag a variable as Acamar describes. BTW you already have a variable for this in the Health Points. If HP<0 then that sprite is destroyed. Dont need another flag, dont need to remove from list. Just have to check the points each time you select a sprite etc.

    Here is a working example that removes the sprite from the list. Sometimes one cant tell which way is best until one does them both. Maybe neither is "better". Maybe the method you understand is best.

    See how I named the Sprites red green blue instead of 1, 2, 3? Then search the list for the name to get the list index. See how there is no relation to the sprite string name and the list index?

    And then this example shows one possibility for how to set the Attacker and Target using a popup toolbar. Or you could just have a toolbar that is always visible. Or whatever you have in mind.

    See how I named the target sprite something that makes sense like TargetListIndex  instead of just i or j ?

    Public Class Form3
        Private GridColRows As New Point(5, 5)
        Private GridRect As Rectangle
        Private GridSize As Size = New Size(50, 50)
        Private MouseMovingColRow, PicColRow, MouseDownPicLocation, MouseDownLocation As Point
        Public Crushed As Boolean
    
        Private WithEvents TankToolbar As New Panel With {.Parent = Me,
            .Size = New Size(GridSize.Width * 3, GridSize.Height), .Visible = False,
            .BackColor = Color.White}
        Private WithEvents SelectTankButton As New Button With {.Parent = TankToolbar,
            .Text = "Target"}
        Private AttackSprite, TargetSprite As String
        Private TankSelectStatus As Integer = 0
    
        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 Sub Pics_MouseDown(sender As Object, e As MouseEventArgs)
            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
                    'remove sprite from list
                    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 Sub
    
        Private Function GetSpriteIndex(theName As String) As Integer
            For i = 0 To Sprites.Count - 1
                If theName = Sprites(i).Pic.Name Then
                    Return i
                End If
            Next
    
            Return -1
        End Function
    
        Private Sub SelectTankButton_Click(sender As Object, e As EventArgs) Handles SelectTankButton.Click
            TankToolbar.Visible = False
            TankSelectStatus = 1
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
                Dim pic = New PictureBox()
                With pic
                    .Name = "Picturebox" & i.ToString
                    .Size = GridSize
                    .Left = i * GridSize.Width
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    'AddHandler .MouseMove, AddressOf pics_MouseMove
                    'AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
            Next
    
            Sprites(0).Pic.BackColor = Color.Red
            Sprites(0).Name = "Sprite Red"
            Sprites(1).Pic.BackColor = Color.Green
            Sprites(1).Name = "Sprite Green"
            Sprites(2).Pic.BackColor = Color.Blue
            Sprites(2).Name = "Sprite Blue"
    
            Sprites(0).HP = 100
            Sprites(1).HP = 10
            Sprites(2).HP = 10
    
            Sprites(0).AP = 10
            Sprites(1).AP = 10
            Sprites(2).AP = 5
    
        End Sub
    End Class
    

    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 12:50 PM
    • Unmarked as answer by Waliur Rahman Saturday, March 25, 2017 12:50 PM
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 3:08 PM
    Saturday, March 25, 2017 11:30 AM

All replies

  • What line of code is causing the error?

    When the above code executes, which messagebox message do you get?

    If you insert a breakpoint at the start of the routine, and step the code through a line at a time, what is the flow of execution?

    Thursday, March 23, 2017 9:28 PM
  • I found the error 

            Sprites(1).HP = Sprites(1).HP - Sprites(2).AP
    

    The program indicates this is the error but when I remove the remove sprite feature it seems to work just fine.


    WRA

    Thursday, March 23, 2017 9:30 PM
  • The program indicates this is the error but when I remove the remove sprite feature it seems to work just fine.

    That would be likely. If you have removed the sprites from the list then you can no longer reference them as a list element.  The game is over.

    The problem comes from the way that you are referring to your sprites.  Because you are using terms like Sprites(1) and Sprites(2) in your code, then that requires that there be a second and a third sprite in the list. As soon as you change the contents of the list you need to ensure that code like that does not execute again, unless you add a new sprite so that there is still a second and a third in the list.

    When the first sprite is killed, what would you expect code like

            Sprites(1).HP = Sprites(1).HP - Sprites(2).AP

    to do?

    Thursday, March 23, 2017 9:41 PM
  • Good question

    In my program I have made a for loop to hold three sprites which means the game isn't absolute over if there is still one tank that remains standing. 

    Once the sprite(1)/tank is destroyed I wouldn't expect the game to be over right away, the game would still continue since there are other tanks still on the whereabouts of the playing field. 


    WRA

    Thursday, March 23, 2017 9:51 PM
  • Once the sprite(1)/tank is destroyed I wouldn't expect the game to be over right away, the game would still continue since there are other tanks still on the whereabouts of the playing field.

    That's the point I was making.  Your code uses literal references to the list items - the second one in the list and the third one in the list.   

    So wherever you have something like
            Sprites(1).HP = Sprites(1).HP - Sprites(2).AP

    you should actually have
             Sprites(X).HP = Sprites(X).HP - Sprites(Y).AP

    where X is the index in the list of the sprite being attacked and Y is the index in the list of the sprite doing the attacking.  When one sprite gets killed, that X value is no longer usable and needs to be recalculated as the next sprite to be attacked.   If you remove a dead sprite from the list then all the values will need to change because everyone shuffles down the list.

    • Proposed as answer by tommytwotrain Saturday, March 25, 2017 4:59 PM
    Thursday, March 23, 2017 9:59 PM
  • ohhhhh I see so during the for loop of the three tanks I made i stated 

    for I = 1 to 3 

    so rather than say sprite 1 call it something like 

    Sprite(I)


    WRA

    Thursday, March 23, 2017 10:19 PM
  • Ill try that as soon as I finish writing up my analysis for my coursework.

    WRA

    Thursday, March 23, 2017 10:36 PM
  • It made sense when I saw how you explained it but now it doesn't because I realised this code here loads three of the tanks: which I have basically called 1.png, 2.png etc 

    1) before when I used the code which I will show bellow it didn't accept what the name of the files were called so one was called heavy panzer.png 

    Option Strict On
    Public Class Form1
        Private GridColRows As New Point(5, 5)
        Private GridRect As Rectangle
        Private GridSize As Size
        Private MouseMovingColRow, PicColRow, MouseDownPicLocation, MouseDownLocation As Point
    
        Public Crushed As Boolean
    
        Private SelectedSprite As Integer = -1
        Private MouseDragStatus As Integer
    
        Private Class Sprite
                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
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Sprites(i).HP = Sprites(i).HP - Sprites(2).AP
    
            Label1.Text = Sprites(1).HP.ToString()
            Label2.Text = Sprites(2).AP.ToString()
    
            If Sprites(1).HP <= 0 Then
                MsgBox("Murked")
                Crushed = False
                'remove sprite from list
                Sprites.Remove(Sprites(1))
            Else
                MsgBox("Still standing")
                Crushed = True
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                DoubleBuffered = True
                For i = 1 To 3
    
                    Dim pic = New PictureBox()
                    With pic
                        .Name = "Pic" & i.ToString
                        .Size = GridSize
                        .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                        AddHandler .MouseDown, AddressOf pics_MouseDown
                        AddHandler .MouseMove, AddressOf pics_MouseMove
                        AddHandler .MouseUp, AddressOf pics_MouseUp
                    End With
    
                    Dim spt As New Sprite
                    spt.Pic = pic
                    spt.ColRow = New Point(i)
                    Sprites.Add(spt)
                Next
    
                Sprites(1).Visible = 2  'frozen
                Sprites(2).Steps = 2
    
                Sprites(1).HP = 10
                Sprites(2).HP = 100
                Sprites(1).AP = 10
                Sprites(2).AP = 5
    
                Label1.Text = Sprites(1).HP.ToString()
                Label2.Text = Sprites(2).AP.ToString()
                Form1_Resize(1, Nothing)
    
    
            End Sub
    
        End Class


    WRA

    Thursday, March 23, 2017 11:18 PM
  • 1) before when I used the code which I will show bellow it didn't accept what the name of the files were called so one was called heavy panzer.png

    The names of the png files is not relevant, except perhaps that it gives a clue as to what you are expecting to happen.

    The problem is that you want to deal with multiple sprites in a list, but you are removing them from the list, so you need to adjust your list indexes to reflect the change. 

    If you have one tank and a varying number of targets, then it is the targets that need to be in the list.  The tank can be its own variable.  So the code in question becomes:
            Sprites(X).HP = Sprites(X).HP - TankSpirite.AP

    where X is the list index of the target that the tank is currently pointed at, and TankSprite is the tank. Obviously, X varies as targets get destroyed.

    Friday, March 24, 2017 1:16 AM
  • Wali,

    The files just happened to be named 1, 2, 3. It has nothing to do with the listindex value.

    ( i )   filename   listindex

     1         1.png         0
     2         2.png         1
     3         3.png         2


    In the following example the for loop index i goes from 3 to 5, but the list index is still the same 0 to 2. A list is similar to an array that starts at zero is all. First item added to the list is at index 0. Second is index 1 etc. In the example Name 3 was first to be added to the list so it is at list index = 0.

    Public Class Form3
        Private MyList As New List(Of String)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim s As String
            For i = 3 To 5
                s = "Name " & i.ToString
                MyList.Add(s)
            Next
            Label1.Text = "Enter a Value from 0 to 2"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If IsNumeric(TextBox1.Text) Then
                Dim n As Integer = CInt(TextBox1.Text)
                If n >= 0 AndAlso n <= 2 Then
                    Label1.Text = MyList(n)
                Else
                    Label1.Text = "Enter a Value from 0 to 2"
                End If
            End If
        End Sub
    
    End Class

    Friday, March 24, 2017 12:57 PM
  • PS Here is another example.

    Enter the index = 1 to the text box click button, the list item at index 1 is removed (name 4) and the exampe shows Name 3 Name 5 remaining in the list (at index 0, 1). Name 4 was at list index 1 so was removed.

    Now click again with list index 1 in the text box and the item at list index 1 = name 5 is removed. Now the list has only Name 3 left at index 0.

    Click again with list index = 1 and get an error because all the list has is one item at index 0 = Name 3.

    Public Class Form3
        Private MyList As New List(Of String)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim s As String
            For i = 3 To 5
                s = "Name " & i.ToString
                MyList.Add(s)
            Next
            ShowList()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If IsNumeric(TextBox1.Text) Then
                Dim n As Integer = CInt(TextBox1.Text)
                MyList.Remove(MyList(n))
                ShowList()
            End If
        End Sub
    
        Private Sub ShowList()
            Label1.Text = ""
            For i = 0 To MyList.Count - 1
                Label1.Text &= MyList(i) & "  "
            Next
        End Sub
    End Class


    Friday, March 24, 2017 1:27 PM
  • @ Tommy woah thanks for the example, I hope by the end of today I could get the tanks to be removed from play which will be the primary objective at the moment so that I could move on to the next few stages of the game. 

    Next week is when my teacher wants us to give in the write up for the coursework I just hope I could finish the development stage by then and learn from the mistakes I made from here too. Thus far I have learnt a lot eversince signing up on msdn forums


    WRA

    Friday, March 24, 2017 2:52 PM
  • I just figured out what was going wrong here. So to the for loop made set 1 to 3 is practically set as just 1 object so basically when it removes it from play it removed all three which crashes vb. (Tommy mention this before, correct me if I'm wrong) 

    Therefore, I need to assign it separately. I just need to know how would I do it to create one per sprite?

    I'm gonna use tank and turret example so I will create a single sprite (tank) in the game and a single Sprite (turret) which will face off against each other and which ever one is destroyed then the sprite is removed. 

    I'm going to leave my computer to think about this problem to see if I think of further solutions to solving this problem so that I can finally sort the issue of selecting the sprite to attack one another rather than just press a button to carry out in doing so. 


    WRA

    Friday, March 24, 2017 3:43 PM
  • "So to the for loop made set 1 to 3 is practically set as just 1 object so basically when it removes it from play it removed all three "

    No I don't think that is correct.

    Assuming i = 1 in this line:

        Sprites(i).HP = Sprites(i).HP - Sprites(2).AP

    Your example works to remove one sprite from index = 1 in the list. Then then next time you click the button it gives the error you show on that same line:

       Sprites(i).HP = Sprites(i).HP - Sprites(2).AP

    Because there is nothing in the list at index 2. One item was removed. Now the list has only items at index 0 and 1.

    First the list of sprites was like this

    0 = sprite red
    1 = sprite green
    2 = sprite blue

    then your removed index 1 so the list is now:

    0 = sprite red
    1 = sprite blue

    there is no longer a sprite green item in the list. And there is no item at index = 2 so you get an error on this line:

     Sprites(i).HP = Sprites(i).HP - Sprites(2).AP

    Friday, March 24, 2017 4:33 PM
  • Ok I can see where you are approaching. I just can't think of a solution to actually solve this. I've tried whatever combinations possible. I don't think the solution is so obvious for me but clear for others. If you don't mind could you try from your side if you could solve the solution? I'm gonna move from this and try to set the range of which the tanks can attack from. 

    WRA

    Friday, March 24, 2017 4:46 PM
  • Ok I can see where you are approaching. I just can't think of a solution to actually solve this. I've tried whatever combinations possible. I don't think the solution is so obvious for me but clear for others. If you don't mind could you try from your side if you could solve the solution? I'm gonna move from this and try to set the range of which the tanks can attack from. 

    WRA


    "could you try from your side if you could solve the solution?"

    Solve what Solution?

    You made a plan and copied some code, added some lines, and the code did not work because it was not written correctly. Now you say there is no solution?

    Seems to me your solution will work if the code is written correctly (not that its the only solution). Acamar and I and the others have explained why the code you keep showing us does not work.

    Now its time for you to relearn, redo, and debug what you wrote until it works. Its not that hard.

    :)

    Friday, March 24, 2017 5:57 PM
  • @tommy 

    Man you make it seem like it is straightforward but If you have done it then I will try too

    Could you if possible just recap what you indicated from before just to remind me where I should be looking and analyse how I could solve this problem


    WRA


    LOL.

    I am not even sure what your question is? I am explained out for now. I have explained for a week. Don't have anything else to say... for now. 

    Maybe Acamar and others have more to say.

    Friday, March 24, 2017 6:18 PM
  • Now idk what I am doing. Ill just ready through what the comments say first then work my way through there. Its a relatively silly error I have done and I can find it if I put my mind to it. 

    Wish me luck that I find the solution to this problem and as soon as I do Ill mark the answer and open a new thready If another obstacle comes in my way. 

    P.S just forget the comment before this one.


    WRA

    Friday, March 24, 2017 6:33 PM
  • I think I am over exaggerating the issue here or looking at it on the bigger picture because of the pressure of the due date the coursework needs to be handed in by. Ill report back as soon as I solve the problem and I read over the comments especially Acamars he said use letters rather than numbers as he stated so that is where the core of my solution must be. 

    I think the matter with me is not reading the comments thoroughly.


    WRA

    Friday, March 24, 2017 6:52 PM
  • @Tommy 

    Hi, when I assign the "Sprite(1) as Sprite(I) instead, it shows that the variable hasn't been assigned"

    But I presume I need to declare it as private as integer = 1 

    so: Private i as integer = 1 

    and so on


    WRA


    Friday, March 24, 2017 7:08 PM
  • @Acamar or Tommy 

    So this is the core of the solution and I didn't see this the whole time... 

    I'm encountering the issue where I rename this 

            Sprites(1).HP = Sprites(1).HP - Sprites(2).AP

    to this: 

             Sprites(i).HP = Sprites(i).HP - Sprites(j).AP

    VB is telling me that the following haven't been declared. Is there some declared variables that I must change such as in the form1_load section? Please reply when you can thanks. 

    I think I am very close to solving this problem as long as I could be pointed to the right lines 



    WRA

    Friday, March 24, 2017 8:20 PM
  • Ill report back as soon as I solve the problem and I read over the comments especially Acamars he said use letters rather than numbers as he stated so that is where the core of my solution must be.

    Not letters instead of numbers.  A variable instead of a literal.  That is, instead of hard coding the number as "1" or "2" you should use a variable such as X so that when the items change their position in the list the variable can be changed, so that there is still a target to shoot at.

    Friday, March 24, 2017 8:33 PM
  • I don't really understand your approach here Acamar could you try put it into context of code. I think the 1, 2 and X and Y is just confusing me. 

    I'm not that good when it comes to understanding concepts as you can see from some past experiences.


    WRA


    Friday, March 24, 2017 8:40 PM
  • VB is telling me that the following haven't been declared. Is there some declared variables that I must change such as in the form1_load section? Please reply when you can thanks.

    If you want to use a variable then you have to establish (declare) it, and you have to ensure it has the appropriate values (initialize it).  As this is going to occur in many places in your code those variables should probably be declared at the form level.

    Then you have to work out how those values might change when sprites get destroyed, and you have to write code that detects when a sprite is destroyed, and change the values as required (or detects that the game is over).

    If you manage those values correctly you don't actually have to remove dead sprites from the list - that might make the code simpler. That is, each sprite maintains its position in the list for the whole game.  You change the value of the sprite being shot at as the target changes, and when a sprite dies you mark it as dead so that it can no longer be selected as a target (that requires a change to the Sprite class definition).


    • Edited by AcamarMVP Friday, March 24, 2017 8:44 PM sp
    Friday, March 24, 2017 8:43 PM
  • Well I dont think labelling the sprite as dead would be helpful since my game would involve the player spawning more tanks during their battles. 

    Back to your other info so you are basically trying to say assign each of the pngs uniquely so 

    x = 1.png 

    y = 2.png

    Sprites(x).hp = Sprites(x).hp - Sprites(y).ap


    WRA

    Friday, March 24, 2017 8:49 PM
  • @Acamar 

    Here we go I made both sprites separate from each other one indicated as the hunter and the other being hunted

    I declared i and j as integers 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
    
            Dim pic = New PictureBox()
            With pic
                .Name = "Pic" & i.ToString
                .Size = GridSize
                .BackgroundImageLayout = ImageLayout.Stretch
                .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                Me.Controls.Add(pic)
                AddHandler .MouseDown, AddressOf pics_MouseDown
                AddHandler .MouseMove, AddressOf pics_MouseMove
                AddHandler .MouseUp, AddressOf pics_MouseUp
            End With
    
            Dim spt As New Sprite
            spt.Pic = pic
            spt.ColRow = New Point(i)
            Sprites.Add(spt)
    
            Dim pic2 = New PictureBox()
            With pic2
                .Name = "Pic" & j.ToString
                .Size = GridSize
                .BackgroundImageLayout = ImageLayout.Stretch
                .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & j.ToString & ".png")
                '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                Me.Controls.Add(pic2)
                AddHandler .MouseDown, AddressOf pics_MouseDown
                AddHandler .MouseMove, AddressOf pics_MouseMove
                AddHandler .MouseUp, AddressOf pics_MouseUp
            End With
    
            Dim spt2 As New Sprite
            spt2.Pic = pic2
            spt2.ColRow = New Point(j)
            Sprites.Add(spt2)
    
    
            Form1_Resize(1, Nothing)
    
            Sprites(i).HP = 10
            Sprites(i).HP = 10
            Sprites(j).AP = 5
            Sprites(j).AP = 5
    
    
        End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Sprites(i).HP = Sprites(i).HP - Sprites(j).AP
    
            Label1.Text = Sprites(i).HP.ToString()
            Label2.Text = Sprites(j).AP.ToString()
    
            If Sprites(i).HP <= 0 Then
                MsgBox("Murked")
                'remove sprite from list
                Sprites.Remove(Sprites(j))
            Else
                MsgBox("Still standing")
            End If
    end sub


    WRA

    Friday, March 24, 2017 9:07 PM
  • Well I dont think labelling the sprite as dead would be helpful since my game would involve the player spawning more tanks during their battles. 

    Back to your other info so you are basically trying to say assign each of the pngs uniquely so 

    x = 1.png 

    y = 2.png

    Sprites(x).hp = Sprites(x).hp - Sprites(y).ap


    WRA

    No.  You already have the images assigned to the sprites as a property of the sprite class instances that you created.   A sprite is much more than just an image.

    The variables that you create are used to indicate which sprite is the shooter and which sprite is the attacker at this particular moment in the game play.   As the game play changes those values change. For example (and it's only one of several circumstances where game play changes) when a sprite dies it can no longer be a target.  So the variable that indicates which sprite in your list of sprites is the current target needs to change.  I don't know how you calculate that change - that's a bit of code you have to work out - but when it changes, code like:

            Sprites(X).HP = Sprites(X).HP - Sprites(Y).AP

    continues to work correctly because X refers to the list index of the new target that the shooter is now aiming at.  I suspect that, in this particular case, only X in that example needs to change, because the shooter (Y) still exists. Whether there are other cases where Y needs to change depends on what the game play is.

    Your comment that you don't think labelling the sprite as dead would work contradicts the initial purpose of this thread - you wanted to remove the tanks from play.  One way to do that is to mark them as dead.  It doesn't have to be removed from the list in order to be removed from play, and leaving it in the list might make the above calculation simpler because items won't be shuffled around in the list. 

    Friday, March 24, 2017 9:16 PM
  • Ohhhhhh I see so basically rather than trying to actually remove this tank I can just label it as dead so when it is long gone any other tank can practically walk over it. 

    In other words, the solution I'm looking for is to remove the sprite from play with an indication to show that it is down for the count? 

    Also the influences of the variable so It needs to be manipulated for Y to attack other than just X so if there are other sprites at present then it should be able to attack Z or B or C and so on. 

    In a nutshell, I need to revamp my if statement to include features to indicate if X isn't available to attack then it attacks B and so on.


    WRA


    Friday, March 24, 2017 9:22 PM
  • Ohhhhhh I see so basically rather than trying to actually remove this tank I can just label it as dead so when it is long gone any other tank can practically walk over it. 

    In other words, the solution I'm looking for is to remove the sprite from play with an indication to show that it is down for the count? 

    Also the influences of the variable so It needs to be manipulated for Y to attack other than just X so if there are other sprites at present then it should be able to attack Z or B or C and so on. 

    In a nutshell, I need to revamp my if statement to include features to indicate if X isn't available to attack then it attacks B and so on.


    WRA


    Wliur, 

    You are not far from understanding OOP with your reply. 

    Know that OOP is based on New construction than Reusing. Reusing was in past cheap, but likewise real life, it has become expensive. Depending from where you life is getting somebody to repair your coffee machine more expensive than to buy a new coffee machine. That is also with OOP and with computing it is not important where you life. 

    If you buy (construct) a new coffee machine, you start using that. You put the old coffee machine at the Garbage (which is automatically done if you construct a new one). When that old machine is collected is not important to you. The Garbage Collector takes care of that. 

    You go on with the new coffee machine and use it as if it was the old by setting its properties in the way you wish. 


    Success
    Cor


    Friday, March 24, 2017 9:35 PM
  • 1) you spelt my name wrong :(

    3) I understand the context you are putting this into 

    4) I haven't seen you in a while, It's great to see you again :)


    WRA


    Friday, March 24, 2017 9:41 PM
  • Ohhhhhh I see so basically rather than trying to actually remove this tank I can just label it as dead so when it is long gone any other tank can practically walk over it.

    Either way, you have to account for the fact that it is dead.  If you leave it in the list then you need code in various places to check if it's dead, and ignore it.  For instance, it can no longer be a target.  If it is removed from the list you need to adjust for the fact that the list sequence has changed.  I think the former is simpler.

    Also the influences of the variable so It needs to be manipulated for Y to attack other than just X so if there are other sprites at present then it should be able to attack Z or B or C and so on. 

    No.  X is a variable.   There is no Z or B or C.  There are a series of sprites in the list, each one identified by its index - an integer: 0, 1, 2 etc.  You set the value of the variable to indicate which item in the list (which index) is the item that the code should refer to.

    In a nutshell, I need to revamp my if statement to include features to indicate if X isn't available to attack then it attacks B and so on.

    I'm not sure what you mean by 'my if statement', but when a sprite is killed you need to
    - identify that a sprite is now dead (the code referred to above). To do this you use the current value of X.
    - work out what sprite (if any) is now the target sprite.  Perhaps there is no target sprite until the player does something.  You could use a X value of -1 to indicate this. Perhaps there are no more targets and the game should end.  Or perhaps there is a new target, so the value of X will be set to the index number of this target in the list of sprites.

    Friday, March 24, 2017 10:14 PM
  • By the way when you said "You are not far from understanding OOP" do you mean that I am starting to understand it better than before or no?

    WRA

    Friday, March 24, 2017 10:14 PM
  • @Acamar

    My brain is now overloading with alphabets this is so much to take in. 

    I'll show you step by step what I am doing now and send screenshots then explain if I'm on the right path of not.


    WRA

    Friday, March 24, 2017 10:21 PM
    1. I think my keyboard starts failing (or my fingers)
    2. Great
    3. Great

    Other reply, 

    Just words, could also be read as "Understand better"


    Success
    Cor


    Friday, March 24, 2017 10:24 PM
  • @Acamar Ill get back to square one the way I actually originated these indexes then process from there. 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
    
            Next
    
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
            Sprites(3).HP = 10
            Sprites(3).AP = 5
    
            Form1_Resize(1, Nothing)
        End Sub

    This is back to square one where the problem actually started from.

    This shows that I start the program and I btw just moved them about but it doesn't matter really. 

     I press the button once it shows this then when it press it again it shows "Murked" and then....

    This part I assume Acamar and others is a recap of the problem and I do apologies if it is


    WRA


    Friday, March 24, 2017 10:32 PM
  • Apologies if they are way tooo big

    WRA

    Friday, March 24, 2017 10:32 PM
  • @Acamar

    I can't give up on this problem because what I'm doing here is testing with a button to select which sprites are attacked and the real plan is using a mouse click to select which tank to attack which is probably different so I want to consolidate I am able to destroy a tank and remove it once destroyed.


    WRA


    Friday, March 24, 2017 10:56 PM
  • Ill get back to square one the way I actually originated these indexes then process from there.

    You need to fix this code first.

    You do this:

            For i = 1 To 3
                Dim spt As New Sprite
                ...
                Sprites.Add(spt)
            Next

    Then you do this:

            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
            Sprites(3).HP = 10
            Sprites(3).AP = 5
    That first bit of code creates Sprites(0), Sprites(1) and Sprites(2) in the list.  Sprites(3) does not exist.  That code will fail. I think everyone assumed this had already been fixed when it was first pointed out.  If your code ever executes beyond that point there is something very magical going on.
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 3:08 PM
    • Unmarked as answer by Waliur Rahman Saturday, March 25, 2017 3:56 PM
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 4:59 PM
    Friday, March 24, 2017 11:53 PM
  • @Acamar 

            Sprites.Remove(Sprites(1))

    This code here I find is dysfunctional from what I have seen. 

    In reality this piece of code isn't removing the sprite from play which is unusual? This could be the core of the problem


    WRA

    Saturday, March 25, 2017 12:01 AM
  • I see. Apologies for lack of clarifications to everyone who have been trying to sort my issue all day or even a week. I may have not addressed it well. 

    WRA

    Saturday, March 25, 2017 12:10 AM
  • @Acamar 

    what actually goes within the elipses? 

    This part:

    Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With


    WRA

    Saturday, March 25, 2017 12:11 AM
  • @Acamar

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load DoubleBuffered = True For i = 1 To 3 Dim spt As New Sprite ... Sprites.Add(spt) end Sub

    Next

    Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
            Sprites(3).HP = 10
            Sprites(3).AP = 5
    Done the first step to your instructions. 


    WRA

    Saturday, March 25, 2017 12:21 AM
  • what actually goes within the elipses? 

    Your existing code.  I only included the part that was relevant to the actual problem - you are creating Sprites(0), Sprites(1) and Sprites(2) in the list.  Then you are referencing Sprites(3), which doesn't exist.

    Saturday, March 25, 2017 12:53 AM
  • This code here I find is dysfunctional from what I have seen.

    Yes.  As has been explained, that is taking one item out of the list, so the other items move down one slot.  Therefore you can still refer to Sprites(1) and you will get a sprite, but it won't be the one that you think it is.

    If you find some other way to indicate that a sprite is no longer alive (as I suggested) then you don't need to remove it from the list, and that solves the problem of everything being moved down one in the list. You could (although I wouldn't recommend it) continue to use literals as the list indexes, because the list hasn't changed.  But in any case you will need code changes elsewhere to avoid using a dead sprite.

    In your final version the result of a kill will be:
    - the sprite is marked as dead. It will not be re-used as either a target or a shooter.
    - the sprite image is changed to rubble.
    - the grid square that the sprite occupied at the time it was killed can no longer be occupied by any other sprite.

    But that is still some way down the track.

    Saturday, March 25, 2017 1:04 AM
  • @Acamar Damn so when the tank is destroyed is it possible to implement any code to actually still spawn a tack in that place still?

    WRA

    Saturday, March 25, 2017 1:47 AM
  • so when the tank is destroyed is it possible to implement any code to actually still spawn a tack in that place still?

    Yes. That's just a copy of the code that created a tank in the first place. But you can't do any of that until you work out the problem with your current use of the list. You need to get your existing code working before considering any enhancements.

    Saturday, March 25, 2017 2:03 AM
  • I see. Quick question the code that Im using now to indicate a tank is destroyed is that practically the same code for when I will change it to selecting sprites with a mouse where I select a sprite and which sprite it should attack

    WRA

    Saturday, March 25, 2017 9:07 AM
  • Quick question the code that Im using now to indicate a tank is destroyed is that practically the same code for when I will change it to selecting sprites with a mouse where I select a sprite and which sprite it should attack

    I'm not sure what you are asking here.  The code you have now for indicating a tank is destroyed isn't working, so it is going to change.  So until you create that code and get it working, it's not possible to know whether or not it's going to be similar to any other code.

    Saturday, March 25, 2017 9:16 AM
  • Alright thanks for answering that question so numero uno is to get the current code to work properly. 

    First part I've done is make an indication with a label to show if the tank is alive or not. here is the screenshot to show. 

    I get where you are taking this, I'll need a png download of rubble to replace the destroyed png.

    I understand what perspective you are taking here. You know that throughout my game I will need to use these types of tank when they are constructed and they can't be removed from play but instead when it is destroyed just to replace it with rubble.


    WRA

    Saturday, March 25, 2017 11:26 AM
  • I see. Quick question the code that Im using now to indicate a tank is destroyed is that practically the same code for when I will change it to selecting sprites with a mouse where I select a sprite and which sprite it should attack

    WRA


    Learn by example you said? Have you learned anything? :)

    So you can either remove the Sprites from the list when destroyed or you can flag a variable as Acamar describes. BTW you already have a variable for this in the Health Points. If HP<0 then that sprite is destroyed. Dont need another flag, dont need to remove from list. Just have to check the points each time you select a sprite etc.

    Here is a working example that removes the sprite from the list. Sometimes one cant tell which way is best until one does them both. Maybe neither is "better". Maybe the method you understand is best.

    See how I named the Sprites red green blue instead of 1, 2, 3? Then search the list for the name to get the list index. See how there is no relation to the sprite string name and the list index?

    And then this example shows one possibility for how to set the Attacker and Target using a popup toolbar. Or you could just have a toolbar that is always visible. Or whatever you have in mind.

    See how I named the target sprite something that makes sense like TargetListIndex  instead of just i or j ?

    Public Class Form3
        Private GridColRows As New Point(5, 5)
        Private GridRect As Rectangle
        Private GridSize As Size = New Size(50, 50)
        Private MouseMovingColRow, PicColRow, MouseDownPicLocation, MouseDownLocation As Point
        Public Crushed As Boolean
    
        Private WithEvents TankToolbar As New Panel With {.Parent = Me,
            .Size = New Size(GridSize.Width * 3, GridSize.Height), .Visible = False,
            .BackColor = Color.White}
        Private WithEvents SelectTankButton As New Button With {.Parent = TankToolbar,
            .Text = "Target"}
        Private AttackSprite, TargetSprite As String
        Private TankSelectStatus As Integer = 0
    
        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 Sub Pics_MouseDown(sender As Object, e As MouseEventArgs)
            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
                    'remove sprite from list
                    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 Sub
    
        Private Function GetSpriteIndex(theName As String) As Integer
            For i = 0 To Sprites.Count - 1
                If theName = Sprites(i).Pic.Name Then
                    Return i
                End If
            Next
    
            Return -1
        End Function
    
        Private Sub SelectTankButton_Click(sender As Object, e As EventArgs) Handles SelectTankButton.Click
            TankToolbar.Visible = False
            TankSelectStatus = 1
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
                Dim pic = New PictureBox()
                With pic
                    .Name = "Picturebox" & i.ToString
                    .Size = GridSize
                    .Left = i * GridSize.Width
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    'AddHandler .MouseMove, AddressOf pics_MouseMove
                    'AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
            Next
    
            Sprites(0).Pic.BackColor = Color.Red
            Sprites(0).Name = "Sprite Red"
            Sprites(1).Pic.BackColor = Color.Green
            Sprites(1).Name = "Sprite Green"
            Sprites(2).Pic.BackColor = Color.Blue
            Sprites(2).Name = "Sprite Blue"
    
            Sprites(0).HP = 100
            Sprites(1).HP = 10
            Sprites(2).HP = 10
    
            Sprites(0).AP = 10
            Sprites(1).AP = 10
            Sprites(2).AP = 5
    
        End Sub
    End Class
    

    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 12:50 PM
    • Unmarked as answer by Waliur Rahman Saturday, March 25, 2017 12:50 PM
    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 3:08 PM
    Saturday, March 25, 2017 11:30 AM
  • Morning Tommy. Ahh I see what you are getting to here. I'll analyse what you have done with your code in comparison to mine and I've been having the problem where it shows it can't find my sprites if I just type their name "Heavy Panzer" so I might import them into a file in vb and access it from there for complexity sake 

    WRA

    Saturday, March 25, 2017 11:46 AM
  • @Tommy

    I know how I understand things better through putting it into the context of a code example so using something as simplistic as the program you just sent, I understand clearly what is going on there in contrast to what I was doing. 

    Thus far I am just seeing how I would make it select the png files that I have rather than the created blocks you have thus far. 


    WRA



    Saturday, March 25, 2017 11:50 AM
  • @Tommy 

    Afternoon, I've played with your code and understand what it is doing. I have applied it to my code for the tanks and the first root where I actually select a tank. It wouldn't show that box to indicate which tank it should be attacking. 

    I debug through the code and found it goes through the first part then it skips the rest. 

        Private Sub Pics_MouseDown(sender As Object, e As MouseEventArgs)
            If TankSelectStatus = 1 Then 'Reads this then skips the rest
                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
                    'remove sprite from list
                    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 Sub
    I'm gonna see If I have named something wrong here with my code because that might be it.


    WRA


    Saturday, March 25, 2017 12:52 PM
  • Wali,

    I don't know if you have made controls in code but I did in the example:

    Private WithEvents TankToolbar As New Panel With {.Parent = Me,
            .Size = New Size(GridSize.Width * 3, GridSize.Height), .Visible = False,
            .BackColor = Color.White}
        Private WithEvents SelectTankButton As New Button With {.Parent = TankToolbar,
            .Text = "Target"}

    So if you use that code you don't add the controls to the form in the VS designer.

    Or you can add them to the form in the designer and leave that code out. Name them as I did.

    PS I am not abandoning the Acamar marking method. I am just showing you a working example of removing from the list.

    Saturday, March 25, 2017 1:03 PM
  • Alright so that following code you have there is perfectly fine.

    Btw this is the first time using controls in codes as the one above 


    WRA

    Saturday, March 25, 2017 1:09 PM
  • @Tommy

    I know how I understand things better through putting it into the context of a code example so using something as simplistic as the program you just sent, I understand clearly what is going on there in contrast to what I was doing. 


    WRA



    That is why I keep telling you learn one thing at a time or you go into overload. Make a one form example of just the problem code. Step out of the game into a single empty form with nothing but the code you are learning. Write the code. Watch the code. Understand the code. Then put it into your big project.


    PS. That's one of the negs of learn by example. Its real hard to get into a large project someone else wrote and understand it. Even when you know it already using someone else's code is hard to learn the code, let alone whatever it is doing.
    Saturday, March 25, 2017 1:10 PM
  • @Tommy 

    Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
                spt.Pic = pic
                spt.ColRow = New Point(i)
    This code here differs to your one and I think this is influencing the outcome of the function you just shown and the pic_mousedown operation


    WRA

    Saturday, March 25, 2017 1:11 PM
  • @Tommy 

    I'll go bit by bit into understanding each section of the code given before Implementing what I just did was a cut and run which should take more time to understand the code before putting it in. 


    WRA

    Saturday, March 25, 2017 1:18 PM
  • @Tommy 

    I'll go bit by bit into understanding each section of the code given before Implementing what I just did was a cut and run which should take more time to understand the code before putting it in. 


    WRA

    Huh?

    Make a one form example using the exact code I posted and run it and step through what I show in the animation.

    Maybe you did all this?

    Put a break point on line one. You know what that is right? I don't know what you know.

    Single step execution one line at a time. Look at the values of the variables before and after each line (hold mouse pointer over variable when in break mode and the value pops up you know that right?).

    My last example does not put an image in the picturebox etc so its not the same as your game. So you cant copy my example into yours. You have to understand the example and then write your own code in your example exactly as your example requires to do what you want.

    Saturday, March 25, 2017 1:27 PM
  • Ohhhh I thought you meant to like just understand code per code sorry. Btw that is what I did with your code I didnt put it into my code I made a new windows form and passed it in there. 

    WRA

    Saturday, March 25, 2017 1:31 PM
  • @Tommy 

    No worries I know what a breakpoint is thank god for that :) 


    WRA

    Saturday, March 25, 2017 1:32 PM
  • Alright I have gone through the code so you are basically saying I can use your example but I need to contrast it to be able to select the image in the picturebox rather than what you have done with colours?

    So when I am doing it, I should remove some stuff in yours and not apply it to mine such as you declared name as string in your class which I wouldn't need.


    WRA

    Saturday, March 25, 2017 1:41 PM
  • Alright I have gone through the code so you are basically saying I can use your example but I need to contrast it to be able to select the image in the picturebox rather than what you have done with colours?

    So when I am doing it, I should remove some stuff in yours and not apply it to mine such as you declared name as string in your class which I wouldn't need.


    WRA

    What I am saying is my example has changed and had removals. You wont easily see them. I named things, took things out. I don't remember. You cant copy it directly.

    What I am saying is you have to understand what you are doing and write your game yourself. You wont understand it if you don't.

    If you don't understand what you are doing make a one form example of just what you are doing and learn and study and change and use something else... then go back to your game when you are sure you have it, and add exactly what is required to do what you want.

    Add one thing at a time. Run it, debug it, get it running with just an outline and a few details. Then keep adding to it.

    Even consider starting over with a new project. It will be a lot better this time around.

    Its not going to happen in 20 minutes.

    Saturday, March 25, 2017 1:51 PM
  • Fair enough Im gonna start a new windows form application I need to look at what I am doing then break down into what to change so It can fit right into my program. 

    WRA

    Saturday, March 25, 2017 1:54 PM
  • @Tommy 

    it is like you said "Its not going to happen in 20 minutes."

    This is something for me to take an awful lot of time to sink in and understand the gist of before actually manipulating its ways to use for my section of code which involve using actual images.


    WRA

    Saturday, March 25, 2017 2:50 PM
  • @Tommy

    YES TOMMY BRING THE CHAMPAGNE OUT LOOOOL JK DONT 

    I FINALLY GOT IT WORKING WOOOOOOOOOOOOO

    I CAN GET THE TANK TO ATTACK ITSELF AND IT GETS DESTROYED AND REMOVE AND IT CAN DESTROY OTHER TANKS IN THE PROCESS.

    Sorry about the over emphasized capitals I'm just happy I solved this problem with the help and support of all of you. This is literally more than half the battle for my program I still gotta set the other kinks such as where to deploy buildings, make sure they are immobile, set a range for attack such as how far can a tank attack from and set a deploy range on the game grid. 


    WRA


    Saturday, March 25, 2017 3:08 PM
  • Just one last question tommy in this section: how am I suppose to move the sprites about and get it to select to remove a sprite from play? Do I just rename one variable or the other under the category of pics_mousedown.

    WRA

    Saturday, March 25, 2017 3:25 PM
  • Just one last question tommy in this section: how am I suppose to move the sprites about and get it to select to remove a sprite from play? Do I just rename one variable or the other under the category of pics_mousedown.

    WRA

    Well that is good to hear if you truly are understanding.

    Mark an answer or two from Acamar as the answer too. Spread around a few helpful marks. A little incentive to help again with your next question.

    As far as moving the sprites goes I don't know exactly. Its not clear to me how you move the sprites. With the mouse? Just using the original example of dragging pics on the grid? You need some method to distinguish between clicking the picture box to select a picturebox to move and to select one for the attacker. You could use LMB to move and RMB to attack. etc.

    Look through my tank example in the other thread now. You will see I have the mousedragging flag. In the mouse down event that flag is set based on other conditions to tell the rest of the code what is happening. There are a couple other flags there too.

    For yours you can just if lmb then move else rmb then attack. Hundreds of ways. Try some. What do you want? What have you seen that was cool? What makes sense? What is intuitive. That's one thing about LMB/RMB clicking, its not as intuitive as a toolbar with a button sticking out at you. So folks will not try rmb because they don't think to try it. The interface should be intuitive and logical. Hidden RMBs and Shift+F9 things are not very. That's what make a great design. One can figure it out without reading the instructions.

    :)

    Saturday, March 25, 2017 4:52 PM
  • Thats music to my ears tommy cheers. 

    Im gonna look back at your tank game and use that feature and rather than rush like I did with this case. I will take my time to understand the content. 


    WRA

    Saturday, March 25, 2017 5:02 PM
  • PS in the other tank example, you should make that form and run it. Search the code for MouseDragStatus. Look at where it is used and how it changes from event to event. Its something in mousedown, mousemove its something+1, mouseup its = 0.

    What else is this application going to do? What other buttons and controls do you need. Other code flags? If you need four more buttons then you might as well make a visible tool bar ie at the top. You have to have some tools. One button can be the select button just like all windows programs, the big Black Arrow. User clicks select, user clicks a tank and moves it or user rmb clicks and gets a menu.

    Or user clicks select button, a new menu appears with select what? ie has Select Attacker, Select Target. Or normal toolbar buttons become active when select is checked. One button Select Attacker, one button Select Target.

    On and on...

    Try some things. See how they work. Sometimes you have to write it and try it to find out. Some things are awkward. Some things are smoooooth.


    PS Its MouseDragStatus in the Tank example.
    Saturday, March 25, 2017 5:28 PM
  • Yep a bit like a trial and error method like someone said in the discussion site that I posted. 

    WRA

    Saturday, March 25, 2017 5:40 PM