Endless Paradigm

Full Version: Visual Basic, omg
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok I'm making a program that has 3 games, everything is ok, it's in portuguese, so i will translate from PT to EN

in this game, I've 3 images: jessica_mini, mensagem and mensagem2
in this game, I've 1 label: mensagens_lidas_label, it shows the number of messages taken by the player

mensagem means message

this isn't working:
        Dim showMS1 As Integer
        Dim showMS2 As Integer
        Dim mensagens_lidas As Integer
        mensagens_lidas = 0
        showMS1 = 1
        showMS2 = 1
        If showMS1 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem.Bounds) Then
                MsgBox("la la la")
                mensagem.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagens_lidas_label.Text = mensagens_lidas
                showMS1 = 0
            End If
        End If

        If showMS2 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem2.Bounds) Then
                MsgBox("la la la")
                mensagem2.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagens_lidas_label.Text = mensagens_lidas
                showMS2 = 0
            End If
        End If

what that should do is:
when the player(jessica_mini) touches the mensagem image, a message box appears » working
when the player(jessica_mini) touches the mensagem2 image, a message box appears » working
the images (mensagem and mensagem2 hide after being touched » working
the label mensagens_lidas_label shows the number of messages taken by the player » it shows 1 after i take the first message but it doesn't show 2 when i take the 2 messages i don't know why

the action jessica_mini.Bounds.IntersectsWith(mensagem2.Bounds) should just be executed when showMSG2 or showMS1 are iqual to 1, but after i say showMSG1/showMSG2 are iqual to 0, the action, still be executed.
I'm guessing the variable mensagens_lidas is declared locally.  For all languages (that I know of) local variables loose their value after the function ends.  Either declare it as Static, or declare it globally.
the variable is declared as static, it isn't on that copy pasted code.

EDIT, mensagens_lidas means read_messages

i fixed this problem:
Quote:the action jessica_mini.Bounds.IntersectsWith(mensagem2.Bounds) should just be executed when showMSG2 or showMS1 are iqual to 1, but after i say showMSG1/showMSG2 are iqual to 0, the action, still be executed.
i just need to fix the label problem, it shows 1 and doesn't change from one after i take the other message... =|
showMS1 = 1
        showMS2 = 1

umm there being set to 1 and then there being checked - there always going to be 1 because each time you run that function it sets them to 1. maybe you meant to make em 0 outside the function.
but i can't set values outside the function =|

as i said, i already fixed that problem.

i need help with the label now.

EDIT, fixed!
o you fixed it - what was wrong?
This is my code now:

Code:
Public Class Form3
    Public mensagens_lidas As String
    Dim showMS1 As Integer
    Dim showMS2 As Integer

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        showMS1 = 1
        showMS2 = 1
    End Sub

    Private Sub Form3_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        Dim jessica_miniRight As Integer
        Dim jessica_miniBottom As Integer
        Dim jessica_miniTop As Integer
        Dim jessica_miniLeft As Integer
        jessica_miniTop = jessica_mini.Top
        jessica_miniLeft = jessica_mini.Left
        jessica_miniRight = jessica_mini.Left + jessica_mini.Width
        jessica_miniBottom = jessica_mini.Top + jessica_mini.Height

        Dim diogo_miniRight As Integer
        Dim diogo_miniBottom As Integer
        Dim diogo_miniTop As Integer
        Dim diogo_miniLeft As Integer
        diogo_miniTop = diogo_mini.Top
        diogo_miniLeft = diogo_mini.Left
        diogo_miniRight = diogo_mini.Left + jessica_mini.Width
        diogo_miniBottom = diogo_mini.Top + jessica_mini.Height

        If e.KeyCode = Keys.Up Then
            jessica_mini.Top = jessica_mini.Top - 10
            diogo_mini.Top = diogo_mini.Top - 14
            diogo_mini.Left = diogo_mini.Left - 14
        End If
        If e.KeyCode = Keys.Down Then
            jessica_mini.Top = jessica_mini.Top + 10
            diogo_mini.Top = diogo_mini.Top + 14
            diogo_mini.Left = diogo_mini.Left + 14
        End If
        If e.KeyCode = Keys.Left Then
            jessica_mini.Left = jessica_mini.Left - 10
            diogo_mini.Left = diogo_mini.Left - 14
            diogo_mini.Top = diogo_mini.Top - 14
        End If
        If e.KeyCode = Keys.Right Then
            jessica_mini.Left = jessica_mini.Left + 10
            diogo_mini.Left = diogo_mini.Left + 14
            diogo_mini.Top = diogo_mini.Top + 14
        End If

        If jessica_mini.Left <= 0 Then
            jessica_mini.Left = 0
        End If
        If jessica_mini.Left >= 424 Then
            jessica_mini.Left = 424
        End If
        If jessica_mini.Top <= 0 Then
            jessica_mini.Top = 0
        End If
        If jessica_mini.Top >= 420 Then
            jessica_mini.Top = 420
        End If

        If diogo_mini.Left <= 0 Then
            diogo_mini.Left = 423
        End If
        If diogo_mini.Left >= 424 Then
            diogo_mini.Left = 1
        End If
        If diogo_mini.Top <= 0 Then
            diogo_mini.Top = 419
        End If
        If diogo_mini.Top >= 420 Then
            diogo_mini.Top = 1
        End If

        If jessica_mini.Bounds.IntersectsWith(diogo_mini.Bounds) Then
            MsgBox("Ganhaste! Comeste o Diogo! Clica Ok para leres a mensagem deixada pelo Diogo")
            MsgBox("Agora que me comeste (ih ih xP), clica OK para passares para o próximo nível.")
            diogo_mini.Hide()
            mensagem.Hide()
            mensagem2.Hide()
            objectivo.Text = "O teu objectivo neste 2º jogo, e' apanhares o maior número possível de Diogos, n te esqueças de apanhar as mensagens!"
            showMS1 = 0
            showMS2 = 0
            jessica_mini.Left = 220
            jessica_mini.Top = 400
            diogo_mini.Left = 100
            diogo_mini.Top = 0
            Form4.Show()
            Me.Hide()
        End If

        If showMS1 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem.Bounds) Then
                MsgBox("Amoro.te muito amor!!! És mt importante para mim. Clica Ok para continuares o jogo.")
                mensagem.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagesn_lidas_label.Text = mensagens_lidas
                showMS1 = 0
            End If
        End If

        If showMS2 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem2.Bounds) Then
                MsgBox("Tu já sabes isto de cor, mas eu vou repetir: ÉS LINDA! xP e quero tar contigo!")
                mensagem2.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagesn_lidas_label.Text = mensagens_lidas
                showMS2 = 0
            End If
        End If

    End Sub

End Class


before: (and something that i can't remember what i changed.)

Code:
Public Class Form3
    Public mensagens_lidas As String
    Dim showMS1 As Integer
    Dim showMS2 As Integer

    Private Sub Form3_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        showMS1 = 1
        showMS2 = 1
        Dim jessica_miniRight As Integer
        Dim jessica_miniBottom As Integer
        Dim jessica_miniTop As Integer
        Dim jessica_miniLeft As Integer
        jessica_miniTop = jessica_mini.Top
        jessica_miniLeft = jessica_mini.Left
        jessica_miniRight = jessica_mini.Left + jessica_mini.Width
        jessica_miniBottom = jessica_mini.Top + jessica_mini.Height

        Dim diogo_miniRight As Integer
        Dim diogo_miniBottom As Integer
        Dim diogo_miniTop As Integer
        Dim diogo_miniLeft As Integer
        diogo_miniTop = diogo_mini.Top
        diogo_miniLeft = diogo_mini.Left
        diogo_miniRight = diogo_mini.Left + jessica_mini.Width
        diogo_miniBottom = diogo_mini.Top + jessica_mini.Height

        If e.KeyCode = Keys.Up Then
            jessica_mini.Top = jessica_mini.Top - 10
            diogo_mini.Top = diogo_mini.Top - 14
            diogo_mini.Left = diogo_mini.Left - 14
        End If
        If e.KeyCode = Keys.Down Then
            jessica_mini.Top = jessica_mini.Top + 10
            diogo_mini.Top = diogo_mini.Top + 14
            diogo_mini.Left = diogo_mini.Left + 14
        End If
        If e.KeyCode = Keys.Left Then
            jessica_mini.Left = jessica_mini.Left - 10
            diogo_mini.Left = diogo_mini.Left - 14
            diogo_mini.Top = diogo_mini.Top - 14
        End If
        If e.KeyCode = Keys.Right Then
            jessica_mini.Left = jessica_mini.Left + 10
            diogo_mini.Left = diogo_mini.Left + 14
            diogo_mini.Top = diogo_mini.Top + 14
        End If

        If jessica_mini.Left <= 0 Then
            jessica_mini.Left = 0
        End If
        If jessica_mini.Left >= 424 Then
            jessica_mini.Left = 424
        End If
        If jessica_mini.Top <= 0 Then
            jessica_mini.Top = 0
        End If
        If jessica_mini.Top >= 420 Then
            jessica_mini.Top = 420
        End If

        If diogo_mini.Left <= 0 Then
            diogo_mini.Left = 423
        End If
        If diogo_mini.Left >= 424 Then
            diogo_mini.Left = 1
        End If
        If diogo_mini.Top <= 0 Then
            diogo_mini.Top = 419
        End If
        If diogo_mini.Top >= 420 Then
            diogo_mini.Top = 1
        End If

        If jessica_mini.Bounds.IntersectsWith(diogo_mini.Bounds) Then
            MsgBox("Ganhaste! Comeste o Diogo! Clica Ok para leres a mensagem deixada pelo Diogo")
            MsgBox("Agora que me comeste (ih ih xP), clica OK para passares para o próximo nível.")
            diogo_mini.Hide()
            mensagem.Hide()
            mensagem2.Hide()
            objectivo.Text = "O teu objectivo neste 2º jogo, e' apanhares o maior número possível de Diogos, n te esqueças de apanhar as mensagens!"
            showMS1 = 0
            showMS2 = 0
            jessica_mini.Left = 220
            jessica_mini.Top = 400
            diogo_mini.Left = 100
            diogo_mini.Top = 0
            Form4.Show()
            Me.Hide()
        End If

        If showMS1 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem.Bounds) Then
                MsgBox("Amoro.te muito amor!!! És mt importante para mim. Clica Ok para continuares o jogo.")
                mensagem.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagesn_lidas_label.Text = mensagens_lidas
                showMS1 = 0
            End If
        End If

        If showMS2 = 1 Then
            If jessica_mini.Bounds.IntersectsWith(mensagem2.Bounds) Then
                MsgBox("Tu já sabes isto de cor, mas eu vou repetir: ÉS LINDA! xP e quero tar contigo!")
                mensagem2.Hide()
                mensagens_lidas = mensagens_lidas + 1
                mensagesn_lidas_label.Text = mensagens_lidas
                showMS2 = 0
            End If
        End If

    End Sub

End Class

yeah you moved the showms ones into the form load function (good idea).
i don't see that much difference really but i would have to go through it in detail. but anyway glad it worked out and thanks for sharing. :)
Reference URL's