Question Two Progress bars ...

PRo-Beaniie

Well-known member
Joined
Mar 17, 2011
Messages
55
Location
Hertford, Hertfordshire, United Kingdom
Programming Experience
3-5
Hey guys,

Im having some trouble with my progress bars in VB.Net. I am currently making a 2d game for a college assignment. The problem is i have one for health and one for reloading the reload bar works fine but when i try to code the health bar i think it conflicts with the other Progress bar ... hope you guys can help heres my recent code ...
VB.NET:
Public Class FormMain
    'DIM controls ...
    Dim sright As Boolean
    Dim sleft As Boolean
    Dim shooterspeed As Integer
    Dim missilespeed As Integer
    Dim EnemySpeed As Integer
    Dim ERight As Boolean
    Dim ELeft As Boolean
    Private Sub FormMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Arrow Key Contorls ...
        If e.KeyValue = Keys.Right Then
            sright = True
            sleft = False
        End If
        If e.KeyValue = Keys.Left Then
            sleft = True
            sright = False
        End If
        If e.KeyValue = Keys.Down Then
            sleft = False
            sright = False
        End If

        If e.KeyValue = Keys.Space And Missile.Visible = False Then
            Missile.Top = Shooter.Top
            Missile.Left = Shooter.Left + (Shooter.Width / 2) - (Missile.Width / 2)
            Missile.Visible = True And (Missile.Top >= Shooter.Height + Shooter.Width) And Me.ClientRectangle.Width
        End If
    End Sub
    Private Sub FormMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Arrow Key Controls...
        If e.KeyValue = Keys.Right Then
            sright = False
        End If
        If e.KeyValue = Keys.Left Then
            sleft = False
        End If
        If e.KeyValue = Keys.Space And Missile.Visible = True Then
            sleft = False
            sright = False
        End If
    End Sub
    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Event On Form Load...
        LoadSettings()
        'Splash Screen Load 
        TriggerhappyLoad.ShowDialog()
    End Sub
    Private Sub LoadSettings()
        'Load Settings Speed Controls ...
        missilespeed = 10
        shooterspeed = 3
        EnemySpeed = 5
        ERight = True
        Missile.Visible = False
    End Sub
    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Do You Want To Quit?
        Dim result As MsgBoxResult
        result = MessageBox.Show("Do you really want to quit?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If result = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
        'Timer Main Variables...
        moveshooter()
        Firemissile()
        CheckHit()
        MoveEnemy()
        Reloadbar()
        [COLOR=darkorange]HealthStatus()[/COLOR]
    End Sub
    Private Sub moveshooter()
        'Shooter Timer Variables ...
        If sright = True And Shooter.Left + Shooter.Width < Me.ClientRectangle.Width Then
            Shooter.Left += shooterspeed
        End If
        If sleft = True And Shooter.Left > Me.ClientRectangle.Left Then
            Shooter.Left -= shooterspeed
        End If
    End Sub
    Private Sub Firemissile()
        'Missile Timer Variables ...
        If Missile.Visible = True Then
            Missile.Top -= missilespeed
        End If
        If Missile.Top + Missile.Height < Me.ClientRectangle.Top Then
            Missile.Visible = False
        End If
    End Sub
    Private Sub CheckHit()
        'Timer Main Collision Code ...
        If (Missile.Top + Missile.Height >= Enemy.Top) And (Missile.Top <= Enemy.Top + Enemy.Height) And (Missile.Left + Missile.Width >= Enemy.Left) And (Missile.Left <= Enemy.Left + Enemy.Width) And Enemy.Visible = True Then
            Enemy.Visible = False
            Missile.Visible = False
        End If
    End Sub
    Private Sub MoveEnemy()
        'Enemy Timer Variables ...
        If ERight = True Then
            Enemy.Left += EnemySpeed
        End If
        If Enemy.Left >= 900 Then
            Enemy.Left = -100
        End If
    End Sub
    Private Sub Reloadbar()
        ReloadingBar.Maximum = 100
        ReloadingBar.Minimum = 0

        If Missile.Visible = True Then
            ReloadingBar.Value = 100
        Else
            Missile.Visible = False
            ReloadingBar.Value = 0
        End If
    End Sub
    Private Sub [COLOR=darkorange]Healthstatus()[/COLOR]
        HealthStatus.Maximum = 100
        HealthStatus.Minimum = 0
    End Sub
End Class
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Instead of posting a whole form's worth of code, most of which will be irrelevant, how about you explain to us what you're actually trying to achieve and what's actually happening? You say that you have a ProgressBar for health and another for reloading, but what does that actually mean? We can make assumptions but your code doesn't support the assumptions I would make. For a start, you appear to have a ProgressBar and a method both called "HealthStatus". Further, you're setting the Minimum and Maximum on the Tick of a Timer and not setting the Value at all. The Minimum and Maximum should generally never change and should be set in the designer.
 

PRo-Beaniie

Well-known member
Joined
Mar 17, 2011
Messages
55
Location
Hertford, Hertfordshire, United Kingdom
Programming Experience
3-5
Sorry ,

Im sorry for not being more clear on the problem but im only a week old to all this stuff. :(
I post the whole code because i genrally dont know what i have to add and where. Our Tutor is new to the software too so im learning off of you guys and loads of other tutorials on the web.

The game is a simple rocket arcade game i have enemys which scroll across the screen and a truck at the bottom which shoots a rocket to the top of the form. Im not to concerned about the relaoding bar because it works fine in the form. dont know how ... it just does...:confused:
The problem im having right now is when ever i try to make a new variable for my healthbar within TimerMain Vb says its healthbar is already declared as 'Friend withevents healthstatus as system.windows.forms.progressbar' in this class' im so confused ....
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
When you add a control to the form, a member variable is declared that you use to refer to that control. You don't need to declare another variable; you just use that one. When you add the ProgressBar to the form and set its Name to HealthBar, you're adding a member variable with that name. That's what you use to refer to that ProgressBar object in code.

You set the Minimum and Maximum properties in the designer, or just leave them with their default values of 0 and 100 if that's appropriate. In code, all you have to do is set the Value property. It controls how much of the bar is filled. You set it to a value between the Minimum and Maximum. If the Minimum is 0 and the Maximum is 100, setting the Value to 50 will half-fill the bar.
 
Top Bottom