none
Limit the number of steps a sprite takes "per turn"

    Question

  • Hello all. 

    I have manage to limit the number of steps the actual sprite can take and once it has made that step, what I want it to do is just stay there and it can't move. 

    For example, it is in the position (4,5) it moves 2 steps across so its new position is (6,5). I would like it to sit at that new position as soon it is moved so the player isn't able to drag it once more. 

    How would I be able to do this?

    I could use a button to test this call it "refresh" so every time I select it then the sprite can move 2 steps again and continuously every time the refresh button is selected. 


    WRA

    Thursday, April 13, 2017 7:29 PM

Answers

  • I would like to use a button since that Is what I will be doing.

    That's where you would adjust the property. Your code will look like this:

    If spt.Visible <> 1 Then
    'move the sprite MouseDownPicLocation = spt.Pic.Location MouseDownLocation = e.Location MouseDownSprite = spt spt.Pic.BringToFront()
    spt.pic.Location = [ new location for the sprite ]
    If [ something ] then
    ' Sprite can't move again
    spt.Visible = 1
    End If
    End If

    You just need to decide what the [ something ] rule is that determines that the sprite cannot move again.

    In the button click event you would set .Visible to 2.  But how would you know which sprite needed to be set to move again, or would the button re-enable all sprites?

    • Marked as answer by Waliur Rahman Sunday, April 16, 2017 11:14 AM
    Saturday, April 15, 2017 10:31 PM

All replies

  • Hi

    Again, if you had a 'Sprite' Class, you could have a Property called (say) MoveAllowed which could easily be set/unset/examined as to whether or not that Sprite can move or not.


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Thursday, April 13, 2017 7:43 PM
    Thursday, April 13, 2017 7:38 PM
  • Alright I see. I have the Class called Sprite it has the following declared as public 

     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


    WRA

    Private Sub Pics_MouseDown(ByVal obj As Object, e As MouseEventArgs)
            If e.Button = MouseButtons.Left Then
                For Each spt As Sprite In Sprites
                    If spt.Pic Is DirectCast(obj, PictureBox) Then
                        If spt.Visible = 2 Then
                            'move the sprite
                            MouseDownPicLocation = spt.Pic.Location
                            MouseDownLocation = e.Location
                            MouseDownSprite = spt
                            spt.Pic.BringToFront()
                        Else
                            'Select the sprite
                            MouseDownSprite = Nothing
                        End If
                        Exit For
                    End If
                Next
            End If
        End Sub
    In addition my private sub which allows the movement of the sprites and in the load form the only thing you need knowledge of is "Sprites(0).steps = 5" so the sprite moves only 5 steps. 

    Thursday, April 13, 2017 7:57 PM
  • Hi

    From what I can see, you already have what you need. The Property Visible, when set to 1 means that Sprite can not move (as I am guessing). If that is correct then to prevent a Sprite from being moived anymore,just set that Property to 1 and whan it is tried to move, check that Property and refuse movement if =1


    Regards Les, Livingston, Scotland

    Thursday, April 13, 2017 8:09 PM
  • For example, it is in the position (4,5) it moves 2 steps across so its new position is (6,5). I would like it to sit at that new position as soon it is moved so the player isn't able to drag it once more. 

    You have to understand the task before you can code it.   What is the rule that means a sprite can no longer move?  You have indicated that a move from (4,5) to (6,5) would mean that it couldn't move any more, but why?  Is it because it moved two spaces? What would happen if it had only moved one space, or three?  Is it because it moved so that it was now sitting at a particular place (eg, moved off the battlefield)? Is it because it is now two steps away from where it started, and it doesn't matter how it got there (eg, ran out of fuel)? Or would any move, any number of spaces, to any square disable the sprite?  A single example is not enough to define the rule, and you can't create the code until you know the rule.

    You will need some way to re-enable movement, but that is a different task that you can think about after you have worked out what the process is to prevent the sprite from moving.

    Thursday, April 13, 2017 9:32 PM
  • Reasonable questions. I know the sprite could assign to move 2 steps on the game board and it doesn't in my program have to move precisely 2 steps it can make only 1 step etc all I gotta do is stick it in its place after moving it that time then by for instance pressing a refresh button it can then move another two steps. 

    You can see I highlighted the movement of the a specific tank just to show. 


    WRA



    Thursday, April 13, 2017 9:40 PM
  • I know the sprite could assign to move 2 steps on the game board and it doesn't in my program have to move precisely 2 steps it can make only 1 step etc all I gotta do is stick it in its place after moving it that time then by for instance pressing a refresh button it can then move another two steps.

    So you have indicated that it could move any number of spaces in one turn.    Now you need to specify what moves prevent it from moving again.  Is it a move of a specific number of spaces  (eg, 2) in one turn or is it when it has moved a total number of spaces (eg, 2) in a series of turns or does the distance not matter and it is the square it lands on that determines it can't move again, or is it something else?

    Thursday, April 13, 2017 10:43 PM
  • It will be determined by the square it lands on. 

    WRA

    Friday, April 14, 2017 1:06 PM
  • It will be determined by the square it lands on.

    So what is the rule?  Is it some property of that square?  Currently, you don't have an object that provides properties for each square, so perhaps you need to define a 'square' object and set up a collection (such as a list) so that for the square the sprite lands on you can look up the collection, get the property and determine if that is the end of the move or not.    OTOH if it is somehow calculated from the square position you could use the Location property of the picture box positioned at that square to determine whether or not it terminates the move.  It depends on what your rule is. 

    Friday, April 14, 2017 9:00 PM
  • I was thinking could it be something within the pics_mousedown sub that could help?

    Here is some of the code which might help reach an answer. 

        Private Sub Pics_MouseDown(ByVal obj As Object, e As MouseEventArgs)
    'Move the sprite about 
            If e.Button = MouseButtons.Left Then
                For Each spt As Sprite In Sprites
                    If spt.Pic Is DirectCast(obj, PictureBox) Then
                        If spt.Visible = 2 Then
                            'move the sprite
                            MouseDownPicLocation = spt.Pic.Location
                            MouseDownLocation = e.Location
                            MouseDownSprite = spt
                            spt.Pic.BringToFront()
                        Else
                            'Select the sprite
                            MouseDownSprite = Nothing
                        End If
                        Exit For
                    End If
                Next
            End If
        End Sub

    This is what the visible feature does. 

        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
    Therefore I was thinking what if during the mousedown event when the new position is confirmed then it will show a msgbox indicating the new position then the visible will be change to 1 = frozen then by pressing a button on the form it changes back to 2 


    WRA

    Saturday, April 15, 2017 7:35 PM
  • I would like to use a button since that Is what I will be doing. 

    How will I be able to use to to "refresh the mousedown" sub so that the sprite can move few steps again. 


    WRA

    Saturday, April 15, 2017 7:59 PM
  • I would like to use a button since that Is what I will be doing.

    That's where you would adjust the property. Your code will look like this:

    If spt.Visible <> 1 Then
    'move the sprite MouseDownPicLocation = spt.Pic.Location MouseDownLocation = e.Location MouseDownSprite = spt spt.Pic.BringToFront()
    spt.pic.Location = [ new location for the sprite ]
    If [ something ] then
    ' Sprite can't move again
    spt.Visible = 1
    End If
    End If

    You just need to decide what the [ something ] rule is that determines that the sprite cannot move again.

    In the button click event you would set .Visible to 2.  But how would you know which sprite needed to be set to move again, or would the button re-enable all sprites?

    • Marked as answer by Waliur Rahman Sunday, April 16, 2017 11:14 AM
    Saturday, April 15, 2017 10:31 PM
  • Th button to be specific would re enable the sprite movement but I wonder is there a way to do it such as refresh this private sub? 

    WRA

    Sunday, April 16, 2017 11:00 AM
  • Got it working 

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each spt As Sprite In Sprites
                If spt.Visible = 1 Then
                    MsgBox("Rebooting")
                    spt.Visible = 2
                End If
            Next
        End Sub

    Now my tanks move 5 steps and as soon as I press the button it will refresh them to move another 5 steps. 


    WRA

    Sunday, April 16, 2017 11:15 AM
  • @Acamar 

    Quick question, the sprite class is in another windows form and I want to use the button in another windows form. 

    How would I use a button in one form to refresh the sprite movements in another form?

    Here is the example. As soon as I press the end turn button then player 2 can move their sprite and vice versa. 


    WRA

    Sunday, April 16, 2017 11:21 AM
  • Quick question, the sprite class is in another windows form and I want to use the button in another windows form.

    Why do you have two forms?

    Code within a form is made accessible from other forms by creating it within a public method (a Sub in this case).   How you create the reference to the form that contains the method depends on how you are creating your form instances.  For instance, if the 'other' form is created using code in your first firm, then the reference to that first form can be passed as an argument to the constructor of the 'other' form.

    Sunday, April 16, 2017 12:56 PM
  • To make a long story short it is just to the second player is able to take control of their units and move it about. Im not gonna expect you to understand this because I lack clarity but to go into it. 

    What the end turn button will do (my intentions) is on the game board when the sprites are frozen then the button refreshes so that they can move about again. 

    This basically: 

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each spt As Sprite In Sprites
                If spt.Visible = 1 Then
                    MsgBox("Rebooting")
                    spt.Visible = 2
                End If
            Next
        End Sub

    Now my tanks move 5 steps and as soon as I press the button it will refresh them to move another 5 steps. 


    WRA


    Sunday, April 16, 2017 2:57 PM
  • To make a long story short it is just to the second player is able to take control of their units and move it about. Im not gonna expect you to understand this because I lack clarity but to go into it. 

    But why do you need a second form to do this?

    You haven't explained how you are creating the instance of that 'other' form.

    Sunday, April 16, 2017 9:43 PM
  • I have a second form because it is a two player game. But yes that is the part I haven't done yet create an instance of the class. I am thinking that maybe not possible thus I might place somewhere in the load form to refresh the sprites movement. Maybe that will be better.

    WRA

    Monday, April 17, 2017 11:08 AM
  • I have a second form because it is a two player game. But yes that is the part I haven't done yet create an instance of the class.

    Unfortunately, that does not provide any useful information.  If you want to call a method in another form then you need a reference to that other form - a variable that allows you to write code like

        MainForm.RebootAllSprites()

    How you get that reference depends on how you create the form instance.  That's the code you haven't shown.

    This is one option: https://vbdotnetblog.wordpress.com/forms/where-did-i-come-from/

    Monday, April 17, 2017 12:48 PM
  • Thanks for the link Acamar it looks really helpful and links to what Im on about with crossover of forms. 

    Ill read it through to see if it answers my question and apply it to my code context. 


    WRA


    Monday, April 17, 2017 1:52 PM