Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Probably a simple code problem
View previous topic :: View next topic  
Author Message
TechDragon
Newbie


Joined: 28 Nov 2003
Posts: 3

PostPosted: Nov 28th, 2003 10:20 AM    Post subject: Probably a simple code problem Reply with quote

Hi,

I'm new to these forums, and relatively new to Visual Basic as well. However, I have progressed far enough to begin making a text-based game using a lot of If commands.

My game is an old-school RPG style, and I'm running into a problem in my Attack scripts. Everything was working fine until I tried to add in a Race selection (Human, Elf, or Ogre) in the beginning. Then, I took my entire attack code, added an If statement to the beginning, copy-pasted it 2 times, and changed some of the variables for damage and HP on the extra 2. I created it in another Form first, and it worked fine. Then I tried incorperating it with main game code, making sure to add the appropriate If Race = 1, Elseif Race = 2, etc... and an extra End If at the end.

On getting into a battle and hitting the Attack button (no matter which Race I selected at the beginning, it gave me an error:

"Compile error: Block If without End If"

...and highlights the End Sub of my attack code. I tried adding an extra End If, even though I double-checked and shouldn't need an extra one. When I tried that, absolutely nothing would happen when I hit the Attack button.

Any help offered would be much appreciated. Thank you.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Nov 30th, 2003 07:40 AM    Post subject: Reply with quote

You probably missed something... Can you post your code so we can have a look and maybe we'll see something?
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
TechDragon
Newbie


Joined: 28 Nov 2003
Posts: 3

PostPosted: Dec 1st, 2003 03:00 PM    Post subject: Reply with quote

For some odd reason, after getting rid of the new code and putting it in all over again from the beginning, it started working. No problems or anything. Weird, eh?

If you still want to see the code, (the now working code Smile) here it is:

Code:
Private Sub cmdAttack_Click()
Randomize
Dim youhithim As Integer
Dim hehityou As Integer
Dim damagetoyou As Integer
Dim damagetohim As Integer
If Race = 1 Then
MsgBox "You attack your enemy!", , "Combat Screen"
youhithim = (10 - 1) * Rnd + 1
If youhithim >= 1 And youhithim <= 8 Then
MsgBox "Your attack hits the enemy!", , "Combat Screen"
youhithim = 1
Else
MsgBox "Your attack misses!", , "Combat Screen"
youhithim = 0
End If
If youhithim = 1 Then
damagetohim = (8 - 1) * Rnd + 1
MsgBox "You do " & damagetohim & " damage to the enemy!", , "Combat Screen"
EnHealth = EnHealth - damagetohim
lblEnHp.Caption = "Enemy HP = " & EnHealth
End If
If EnHealth <= 0 Then
MsgBox "Your enemy has been defeated!", , "Victory Screen"
MsgBox "Congratulations! You have won the fight!", , "Victory Screen"
lblMessage.Caption = " "
Encounter = 0
Fight = 0
cmdAttack.Visible = False
lblEnHp.Caption = " "
Health = Health + 5
lblHP.Caption = "HP = " & Health
ElseIf EnHealth >= 1 Then
MsgBox "Your enemy attacks you!", , "Combat Screen"
hehityou = (10 - 1) * Rnd + 1
If hehityou >= 1 And hehityou <= 8 Then
MsgBox "Your enemy's attack hits you!", , "Combat Screen"
hehityou = 1
Else
MsgBox "His attack misses!", , "Combat Screen"
hehityou = 0
End If
If hehityou = 1 Then
damagetoyou = (6 - 1) * Rnd + 1
MsgBox "Your enemy does " & damagetoyou & " damage to you!", , "Combat Screen"
Health = Health - damagetoyou
lblHP.Caption = "HP = " & Health
End If
If Health <= 0 Then
MsgBox "Your enemy has defeated you!", , "Death Screen"
MsgBox "How unfortunate. You have lost the fight.", , "Death Screen"
Unload Me
End If
End If
ElseIf Race = 2 Then
MsgBox "You attack your enemy!", , "Combat Screen"
youhithim = (10 - 1) * Rnd + 1
If youhithim >= 1 And youhithim <= 9 Then
MsgBox "Your attack hits the enemy!", , "Combat Screen"
youhithim = 1
Else
MsgBox "Your attack misses!", , "Combat Screen"
youhithim = 0
End If
If youhithim = 1 Then
damagetohim = (6 - 2) * Rnd + 2
MsgBox "You do " & damagetohim & " damage to the enemy!", , "Combat Screen"
EnHealth = EnHealth - damagetohim
lblEnHp.Caption = "Enemy HP = " & EnHealth
End If
If EnHealth <= 0 Then
MsgBox "Your enemy has been defeated!", , "Victory Screen"
MsgBox "Congratulations! You have won the fight!", , "Victory Screen"
lblMessage.Caption = " "
Encounter = 0
Fight = 0
cmdAttack.Visible = False
lblEnHp.Caption = " "
Health = Health + 5
lblHP.Caption = "HP = " & Health
ElseIf EnHealth >= 1 Then
MsgBox "Your enemy attacks you!", , "Combat Screen"
hehityou = (10 - 1) * Rnd + 1
If hehityou >= 1 And hehityou <= 7 Then
MsgBox "Your enemy's attack hits you!", , "Combat Screen"
hehityou = 1
Else
MsgBox "His attack misses!", , "Combat Screen"
hehityou = 0
End If
If hehityou = 1 Then
damagetoyou = (6 - 1) * Rnd + 1
MsgBox "Your enemy does " & damagetoyou & " damage to you!", , "Combat Screen"
Health = Health - damagetoyou
lblHP.Caption = "HP = " & Health
End If
If Health <= 0 Then
MsgBox "Your enemy has defeated you!", , "Death Screen"
MsgBox "How unfortunate. You have lost the fight.", , "Death Screen"
Unload Me
End If
End If
ElseIf Race = 3 Then
MsgBox "You attack your enemy!", , "Combat Screen"
youhithim = (10 - 1) * Rnd + 1
If youhithim >= 1 And youhithim <= 7 Then
MsgBox "Your attack hits the enemy!", , "Combat Screen"
youhithim = 1
Else
MsgBox "Your attack misses!", , "Combat Screen"
youhithim = 0
End If
If youhithim = 1 Then
damagetohim = (10 - 2) * Rnd + 2
MsgBox "You do " & damagetohim & " damage to the enemy!", , "Combat Screen"
EnHealth = EnHealth - damagetohim
lblEnHp.Caption = "Enemy HP = " & EnHealth
End If
If EnHealth <= 0 Then
MsgBox "Your enemy has been defeated!", , "Victory Screen"
MsgBox "Congratulations! You have won the fight!", , "Victory Screen"
lblMessage.Caption = " "
Encounter = 0
Fight = 0
cmdAttack.Visible = False
lblEnHp.Caption = " "
Health = Health + 5
lblHP.Caption = "HP = " & Health
ElseIf EnHealth >= 1 Then
MsgBox "Your enemy attacks you!", , "Combat Screen"
hehityou = (10 - 1) * Rnd + 1
If hehityou >= 1 And hehityou <= 9 Then
MsgBox "Your enemy's attack hits you!", , "Combat Screen"
hehityou = 1
Else
MsgBox "His attack misses!", , "Combat Screen"
hehityou = 0
End If
If hehityou = 1 Then
damagetoyou = (6 - 1) * Rnd + 1
MsgBox "Your enemy does " & damagetoyou & " damage to you!", , "Combat Screen"
Health = Health - damagetoyou
lblHP.Caption = "HP = " & Health
End If
If Health <= 0 Then
MsgBox "Your enemy has defeated you!", , "Death Screen"
MsgBox "How unfortunate. You have lost the fight.", , "Death Screen"
Unload Me
End If
End If
End If
End Sub


That's the code for the attack button. Obviously some of the variables are global, and declared/defined elsewhere. Thanks for all the help though. You were right, it must have just been something I missed.

(Note: In retrospect, maybe I shouldn't have posted my entire attack code. It does made for a rather long post. Sad )
Back to top
View user's profile Send private message Send e-mail Visit poster's website
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Dec 1st, 2003 06:25 PM    Post subject: Reply with quote

Oh well, the important thing is that you got it working Wink Have fun Smile
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
wezel
Freshman


Joined: 09 Oct 2003
Posts: 30

PostPosted: Dec 9th, 2003 11:42 AM    Post subject: Reply with quote

If you use tabs, it would be much simpler to find bugs.
_________________
Real Programmers don't comment their code - it's hard to write, it should be hard to read
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic