Results 1 to 14 of 14

Thread: [RESOLVED] moving graphics(animation) question

  1. #1

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Resolved [RESOLVED] moving graphics(animation) question

    hey guys.

    i recently posted a thread about the same project, but decided this was a more specific question, so i decided to open a new thread!

    My question regards the graphics class in visual basic 2008. Ive searched the internet low and high for an answer to this, but i cannot find anything! I am attempting to 'shoot' a circle drawn on the form when the user presses the 'r' key. if a string property is set to right, the graphic should be drawn, then updated to different x coordinates based on an interval. if the property is 'up' or 'down' then the y coordinate of the graphic should be changed based on another interval. Now i cannot use one preset timer, because i would be able to shoot only one bullet at a time, which would not work!

    Does anyone know how i can either create an array of graphics(bullets) and add to the list every time the user presses r, then using one preset timer, update them all, or simple create a timer every time a bullet is created, then update the bullet position with its own timer.

    Thanks in advanced!

  2. #2
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    Well, it depends on how you are using them. Are they Shapes? Or Pictureboxes? Or simply just a Location and you paint it onto the form. This is an example shows how you would do it with a single timer, and a picture box. Hopefully that is what you're looking for.

    Code:
    Public Class Form1
        Dim Bullets As New ArrayList 'Create a new array list to hold our "bullets"
        Dim Direction As String = "up"
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.R Then 'When a key is pressed, check to see if it is the "R" Key.
                Dim bullet As New PictureBox 'Creat a new picturebox
                bullet.Parent = Me 'Sets the picture box so its a part of the form.
                bullet.Size = New Point(10, 10) 'Sets the size.
                bullet.Location = New Point(Me.Width / 2 + (bullet.Width / 2), Me.Height / 2 + (bullet.Width / 2)) 'Sets the location
                bullet.BackColor = Color.Black 'Sets the color of the picture box
                bullet.BringToFront() 'Brings it to the front so its ontop of the other picture boxes
                bullet.Visible = True 'Makes it visible
                Bullets.Add(bullet) 'Adds it to the array list.
            End If
        End Sub
    
        Private Sub Tick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tick.Tick
            For Each bullet As PictureBox In Bullets 'For every "bullet" that is a picturebox in our array list, do the following
                If Direction = "down" Then 'Checks the direction.
                    bullet.Top += 1 'This is your speed inteveral.
                    If bullet.Top >= Me.Height Then 'This is a check to see if the bullet went out of bounds.
                        bullet.Visible = False 'Hides the bullet
                        bullet.SendToBack() 'Sends the bullet to back
                        bullet.Dispose() 'Gets rid of the bullet (mabye? Not to sure, I just know I use it and it works.)
                    End If
                ElseIf Direction = "up" Then
                    bullet.Top -= 1
                    If bullet.Bottom <= 0 Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                ElseIf Direction = "left" Then
                    bullet.Left -= 1
                    If bullet.Right <= 0 Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                ElseIf Direction = "right" Then
                    bullet.Left += 1
                    If bullet.Left >= Me.Width Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                End If
            Next
        End Sub
    End Class

  3. #3

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    Thanks for replying!

    I tried this code out, and got a few problems :[ i can shoot when the direction is right, but when i change direction while the timer is enabled, the bullet changed direction as well. I tried to fix this by making four variables as booleans, and setting them as true when the direction was changed, but the results seemed even more random. after a few tweaks in your code, i seemed to get nowhere. Maybe i could use a timer for each direction?

  4. #4
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    Actually, what you could do, is just use another array list, an array of strings instead, and assign it the direction you want. Then you can call array list and check what direction it is in. Or, you could make a new class of "Picturebox" for easier reference. So you could call the code like so:
    Code:
    if mycustombullet.direction = "up" then
    'do events
    end if
    Like so, I'll type up a quick example of that, I'll edit this post when it's ready.

    EDIT:
    I added a new Picturebox class, with a public variable that can be called form outside the class so you can set the direction of the bullet. Just add your "edits" and it should work the way you want. Full code below:

    Code:
    Public Class Form1
        Dim Bullets As New ArrayList 'Create a new array list to hold our "bullets"
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.R Then 'When a key is pressed, check to see if it is the "R" Key.
                Dim bullet As New MyCustomPicturebox 'Creat a new picturebox
                bullet.Parent = Me 'Sets the picture box so its a part of the form.
                bullet.Size = New Point(10, 10) 'Sets the size.
                bullet.Location = New Point(Me.Width / 2 + (bullet.Width / 2), Me.Height / 2 + (bullet.Width / 2)) 'Sets the location
                bullet.BackColor = Color.Black 'Sets the color of the picture box
                bullet.BringToFront() 'Brings it to the front so its ontop of the other picture boxes
                bullet.Visible = True 'Makes it visible
                bullet.Direction = "up"
                Bullets.Add(bullet) 'Adds it to the array list.
            End If
        End Sub
    
        Private Sub Tick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tick.Tick
            For Each bullet As MyCustomPicturebox In Bullets 'For every "bullet" that is a picturebox in our array list, do the following
                If bullet.Direction = "down" Then 'Checks the direction specific to the "bullet".
                    bullet.Top += 1 'This is your speed inteveral.
                    If bullet.Top >= Me.Height Then 'This is a check to see if the bullet went out of bounds.
                        bullet.Visible = False 'Hides the bullet
                        bullet.SendToBack() 'Sends the bullet to back
                        bullet.Dispose() 'Gets rid of the bullet (mabye? Not to sure, I just know I use it and it works.)
                    End If
                ElseIf bullet.Direction = "up" Then
                    bullet.Top -= 1
                    If bullet.Bottom <= 0 Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                ElseIf bullet.Direction = "left" Then
                    bullet.Left -= 1
                    If bullet.Right <= 0 Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                ElseIf bullet.Direction = "right" Then
                    bullet.Left += 1
                    If bullet.Left >= Me.Width Then
                        bullet.Visible = False
                        bullet.SendToBack()
                        bullet.Dispose()
                    End If
                End If
            Next
        End Sub
    End Class
    
    Public Class MyCustomPicturebox 'Creates a new class that inherits the picturebox control.
        Inherits PictureBox
        Public Direction As String = "" 'Adds a public variable to be called outside the class.
    End Class
    Last edited by Mal1t1a; Jan 2nd, 2010 at 04:58 PM.

  5. #5

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    hmm.. il take a look. Thanks!

    EDIT:
    how do i edit the new picturebox array for directions? like this?

    Dim bullet As New MyCustomPicturebox 'Creat a new picturebox
    bullet.direction = "right"
    Last edited by manbearpig001; Jan 2nd, 2010 at 05:13 PM.

  6. #6
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    That is how you would edit it. Does it not work? I haven't tried the actual code myself, I just assumed.

  7. #7

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    it says direction is not declared. unless i use the for each loop. :[

  8. #8
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    Are you trying to edit one of the bullets individually?

  9. #9

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    well.. i mean i cant use a timer to update each bullet unless either the bullets themselves are indexed, or i can call different directions for each bullet. right now the problem is that when i fire while facing right, then move down, the bullets seem to want to go down..

    its very glitchy. theres a lot of random movement, and i think its because each individual bullet isnt taken care of with the timer.

    any ideas on how to do so? i was thinking make a new timer for each bullet, but apparently that isnt necessary.

  10. #10

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    AAHHH i figured it out!!!!!!!!!!

    so what i finally did is create another string, then using your code, updated the string when the keys were pressed to move the player! then i simply created a new class once and made its value the string's value.

    + rep thank you so much for your help.

  11. #11
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    You are welcome, and be-sure to mark the thread as [Resolved].

  12. #12

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: moving graphics(animation) question

    haha how do i mark it as resolved?

  13. #13
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: moving graphics(animation) question

    At the original post, you'll see, "Thread Tools", click that, then click "Mark Thread as Resolved"

  14. #14

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: [RESOLVED] moving graphics(animation) question

    Thanks man :] really helped.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width