If block not working
Blitz3D Forums/Blitz3D Beginners Area/If block not working
| ||
I'm writing a game and for some odd reason my code wont work. The first if block works perfectly but the the others refuse to work and there is no difference in code execution at least not what I can see. I don't know if I'm missing some thing. This one works perfectly, this starts the game if the selector is over the Play Game option on the menu. If Menu\SelectY = 107 And KeyHit(K_ENTER) Then Menu\MenuOn = 0 NewGame End If This one just refuses to work. It loads a saved game. There is no deference between this one and the one at the top, other then the number of the selector position. If Menu\SelectY = 137 And KeyHit(K_ENTER) Then LoadGame Menu\MenuOn = 0 End If I don't get it, why would one work and not the other. I even have a debug mode in the game to check it the variables actually add up and have the right number when their suppose to. This doesn't make any sense. Is it possibly a bug in Blitz3D? Or I'm I just not seeing the problem? |
| ||
Take the then out :) IF Menu\SelectY = 137 And KeyHit(K_ENTER) Endif Should work :) |
| ||
That defeats the purpose. I need the code to work, not removed. Hold on. I think I just remembered an old programming trick. I have the order of the code backwards. The if block for the load game should be above the play game block. I gonna try some thing first. |
| ||
OK, that didn't work. Put it this way If Menu\SelectY = 227 And KeyHit(K_ENTER) Then DoEnd End If If Menu\SelectY = 137 And KeyHit(K_ENTER) Then LoadGame Menu\MenuOn = 0 End If If Menu\SelectY = 107 And KeyHit(K_ENTER) Then Menu\MenuOn = 0 NewGame End If [code] or that way, doesn't work. [code] If Menu\SelectY = 107 And KeyHit(K_ENTER) Then Menu\MenuOn = 0 NewGame End If If Menu\SelectY = 137 And KeyHit(K_ENTER) Then LoadGame Menu\MenuOn = 0 End If If Menu\SelectY = 227 And KeyHit(K_ENTER) Then DoEnd End If The extra if block is to exit the game. It's like Blitz3D is ignoring the if blocks after the first one. I just don't get it. every other if block in the game works perfectly except these. |
| ||
OK, I tried it like this and now it's working as desired.If KeyHit(K_ENTER) Then If Menu\SelectY = 107 Then Menu\MenuOn = 0 NewGame End If If Menu\SelectY = 137 Then LoadGame Menu\MenuOn = 0 End If If Menu\SelectY = 227 Then DoEnd End If End If But it's sill a mystery to me as to why it ignore the if blocks with the first method. Oh well, at least it's working now. |
| ||
Read the documentation for KeyHit to see why this happens. |
| ||
OK, I see. I'll change the code and see if that does the trick. Thanks |
| ||
KeyHit craps-out bigtime, I came up with a better way quite a few years back and I'm sure quite a few other programmers on here probably came up with something the same or similar. Put this at the beginning of your source code. Use KeyIsHit in place of KeyHit. ;-) ;************************************************ ;KeyHits Begin ;Better than the built-in KeyHit function. Dim kHits(256) Function KeyIsHit(k) Local flag = False If Not KeyDown(k) = True Then kHits(k) = False If KeyDown(k) = True And kHits(k) = False kHits(k) = True flag = True EndIf Return flag End Function ;KeyHits End ;************************************************ |
| ||
I know there's only 237 keycodes. No real reason for me defining the kHits array size as 256, it's just one of my annoying little habbits. :D |