-
I have a variable HS which contains the a numer, and I have the following to compare it:
Hs1, H2, H3 contains first, second and third place saved in the registry which is "loaded" in to Hs1, Hs2, Hs3 in the Form_Load
Code:
If Hs > Hs1 Then
NewScore = InputBox("You placed first with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "1st", Hs
SaveSetting "PingPong", "Highscores", "Name1", NewScore
Else
If Hs > Hs1 Then
NewScore = InputBox("You placed second with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "2nd", Hs
SaveSetting "PingPong", "Highscores", "Name2", NewScore
Else
If Hs > Hs2 Then
NewScore = InputBox("You placed third with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "3rd", Hs
SaveSetting "PingPong", "Highscores", "Name3", NewScore
End If
End If
End If
But even if Hs is < Hs1, it still tells me that I have the first place!! What am I doing wrong
I really hope you can help me out! :)
-
Your variables need to be set to long, I think that maybe all that is happening. Examine the following example to see if you understand what I mean:
Code:
Private Sub Command1_Click()
Dim Hs As Long, Hs1 As Long, Hs2 As Long
Hs = Text1.Text
Hs1 = 500
Hs2 = 200
If Hs >= Hs2 And Hs <= Hs1 Then
MsgBox "You have a high score of " & Hs & " a second place standing."
ElseIf Hs > Hs1 Then
MsgBox "You have a high score of " & Hs & " a first place standing."
Else
MsgBox "You did not place a high score"
End If
End Sub
-
If I set the variabled Hs1, Hs and Hs3 to long, I get a type mismatch...
Here is the entire code:
Code:
Dim Hs
Dim FallCheck
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyRight: GoRight
Case vbKeyLeft: GoLeft
Case vbKeyF1: Play
Case vbKeyH: HighScoreList.Show 1
Case vbKeyEscape: End
End Select
End Sub
Sub Play()
If Not FallCheck = 3 Then
Exit Sub
Else
Hs = 0
Score.Caption = "Score: " & "0"
FallCheck = 0
Life1.BorderColor = &HFFFF&
Life2.BorderColor = &HFFFF&
Life3.BorderColor = &HFFFF&
lblGameOver.Visible = False
PlayAgain.Visible = False
Timer1.Enabled = True
Timer1.Interval = 20
Pa.Enabled = False
Pa2.Enabled = False
Hs1 = GetSetting("PingPong", "Highscores", "1st")
If Hs1 = "" Then
SaveSetting "PingPong", "Highscores", "1st", "0"
HighScoreList.Score1.Caption = Hs1
End If
'Retrieve second score
Hs2 = GetSetting("PingPong", "Highscores", "2nd")
If Hs3 = "" Then
SaveSetting "PingPong", "Highscores", "2nd", "0"
HighScoreList.Score2.Caption = Hs2
End If
'Retrieve third score
Hs3 = GetSetting("PingPong", "Highscores", "3rd")
If Hs3 = "" Then
SaveSetting "PingPong", "Highscores", "3rd", "0"
HighScoreList.Score3.Caption = Hs3
End If
'Retrieve first name
Name1 = GetSetting("PingPong", "Highscores", "Name1")
If Name1 = "" Then
SaveSetting "PingPong", "Highscores", "Name1", "No name"
End If
'Retrieve second name
Name2 = GetSetting("PingPong", "Highscores", "Name2")
If Name2 = "" Then
SaveSetting "PingPong", "Highscores", "Name2", "No name"
End If
'Retrieve third name
Name3 = GetSetting("PingPong", "Highscores", "Name3")
If Name3 = "" Then
SaveSetting "PingPong", "Highscores", "Name3", "No name"
End If
End If
End Sub
Sub GoRight()
Line1.X1 = Line1.X1 + 160
Line1.X2 = Line1.X2 + 160
End Sub
Sub GoLeft()
Line1.X1 = Line1.X1 - 160
Line1.X2 = Line1.X2 - 160
End Sub
Private Sub Form_Load()
Dim Hs1, Hs2, Hs3
Dim Name1, Name2, Name3
'Retrieve first score
Hs1 = GetSetting("PingPong", "Highscores", "1st")
If Hs1 = "" Then
SaveSetting "PingPong", "Highscores", "1st", "0"
HighScoreList.Score1.Caption = Hs1
End If
'Retrieve second score
Hs2 = GetSetting("PingPong", "Highscores", "2nd")
If Hs3 = "" Then
SaveSetting "PingPong", "Highscores", "2nd", "0"
HighScoreList.Score2.Caption = Hs2
End If
'Retrieve third score
Hs3 = GetSetting("PingPong", "Highscores", "3rd")
If Hs3 = "" Then
SaveSetting "PingPong", "Highscores", "3rd", "0"
HighScoreList.Score3.Caption = Hs3
End If
'Retrieve first name
Name1 = GetSetting("PingPong", "Highscores", "Name1")
If Name1 = "" Then
SaveSetting "PingPong", "Highscores", "Name1", "No name"
End If
'Retrieve second name
Name2 = GetSetting("PingPong", "Highscores", "Name2")
If Name2 = "" Then
SaveSetting "PingPong", "Highscores", "Name2", "No name"
End If
'Retrieve third name
Name3 = GetSetting("PingPong", "Highscores", "Name3")
If Name3 = "" Then
SaveSetting "PingPong", "Highscores", "Name3", "No name"
End If
End Sub
Private Sub Pa_Timer()
PlayAgain.Visible = True
Pa.Enabled = False
Pa2.Enabled = True
End Sub
Private Sub Pa2_Timer()
PlayAgain.Visible = False
Pa.Enabled = True
Pa2.Enabled = False
End Sub
Private Sub Timer1_Timer()
RndGenCoord = Int((9000 * Rnd) + 1000)
RndGenSpeed = Int((25 * Rnd) + 5)
Line2.Y1 = Line2.Y1 + 120
Line2.Y2 = Line2.Y2 + 120
If CheckCatch = True Then
Hs = Hs + 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = RndGenCoord
Line2.X2 = RndGenCoord
Line2.Y1 = 2280
Line2.Y2 = 2280
If Line2.X1 >= Me.ScaleWidth Or Line2.X1 <= 0 Then
Line2.X2 = Int(Me.ScaleWidth / 2)
Line2.X2 = Line2.X1
End If
Timer1.Interval = RndGenSpeed
Timer1.Enabled = True
End If
If Line2.Y1 = Line1.Y1 Then
Hs = Hs - 1
Score.Caption = "Score: " & Hs
Timer1.Interval = 0
Timer1.Enabled = False
Line2.X1 = RndGenCoord
Line2.X2 = RndGenCoord
Line2.Y1 = 2280
Line2.Y2 = 2280
FallCheck = FallCheck + 1
If FallCheck = 1 Then
Life1.BorderColor = &H0&
End If
If FallCheck = 2 Then
Life2.BorderColor = &H0&
End If
If FallCheck = 3 Then
Life3.BorderColor = &H0&
lblGameOver.Visible = True
Pa.Enabled = True
If Hs >= Hs2 And Hs <= Hs1 Then
NewScore = InputBox("You placed second with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "2nd", Hs
SaveSetting "PingPong", "Highscores", "Name2", NewScore
ElseIf Hs > Hs1 Then
NewScore = InputBox("You placed first with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "1st", Hs
SaveSetting "PingPong", "Highscores", "Name1", NewScore
Else
NewScore = InputBox("You placed third with " & Hs & " points! Type in your name!")
SaveSetting "PingPong", "Highscores", "3rd", Hs
SaveSetting "PingPong", "Highscores", "Name3", NewScore
End If
Exit Sub
GameOver
End If
Timer1.Interval = RndGenSpeed
Timer1.Enabled = True
End If
End Sub
Sub GameOver()
Timer1.Interval = 0
Timer1.Enabled = False
lblGameOver.Visible = True
End Sub
Private Function CheckCatch() As Boolean
If Line2.Y1 >= Line1.Y1 Then
If Line2.Y1 > Line1.Y1 Then
CheckCatch = False
Exit Function
End If
If (Line2.X1 >= Line1.X1 And Line2.X1 <= Line1.X2) Or _
(Line2.X2 >= Line1.X1 And Line2.X2 <= Line1.X2) Then _
CheckCatch = True
End If
End Function
Private Sub TitleTim1_Timer()
Title.ForeColor = &HFFFF&
TitleTim1.Enabled = False
TitleTim2.Enabled = True
End Sub
Private Sub TitleTim2_Timer()
Title.ForeColor = &HFF&
TitleTim1.Enabled = True
TitleTim2.Enabled = False
End Sub
-
I believe that to be that the GetSetting function is returning a string rather than a long. Place CLng round each of your getsetting functions and that should take care of it.
Exampe:
Code:
Hs2 = CLng(GetSetting("PingPong", "Highscores", "2nd"))
-
Yep! that worked! :)
Thanks my friend! :)
-
Glad to have help! If you need anything else, don't hesitate to ask.
-
Thank you! And the very same to you! :)