Issue with assigning variable
BlitzMax Forums/BlitzMax Beginners Area/Issue with assigning variable
| ||
I'll keep this as simple as possible. The code will NOT assign the value of "UpLeft" to the bullet field d (thus, it does not trigger my If statement for bullet direction). The variable, move_dir$, IS being assigned the value of "UpLeft" (it shows at the top of the screen when running the application). Every other direction works as intended. Thank you for your help! The Code: ------------------------- Graphics 640,480,16,2 timer = CreateTimer(60) AutoMidHandle True img_squirrel = LoadImage("images/squirrel.png") Type bullet Field x Field y Field d$ End Type Type alien Field hp Field x Field y Field e End Type x = 100 y = 100 SetBuffer BackBuffer() For z = 1 To 7 a.alien = New alien a\x = Rand(600) a\y = Rand(400) a\hp = Rand(100) a\e = 1 Next While Not KeyDown(1) Cls DrawImage img_squirrel,x,y If KeyDown(203) Then x = x - 3 move_dir$ = "Left" EndIf If KeyDown(205) Then x = x + 3 move_dir$ = "Right" EndIf If KeyDown(200) Then y = y - 3 move_dir$ = "Up" If KeyDown(203) Then move_dir$ = "UpLeft" If KeyDown(205) Then move_dir$ = "UpRight" EndIf If KeyDown(208) Then y = y + 3 move_dir$ = "Down" If KeyDown(205) Then move_dir$ = "DownRight" If KeyDown(203) Then move_dir$ = "DownLeft" EndIf Text 50,50,move_dir$ If KeyHit(57) Then b.bullet = New bullet b\x = x b\y = y b\d = move_dir$ End If For b.bullet = Each bullet If b\d$ = "Left" Then b\x = b\x - 10 If b\d$ = "Right" Then b\x = b\x + 10 If b\d$ = "Up" Then b\y = b\y - 10 If b\d$ = "Down" Then b\y = b\y + 10 If b\d$ = "DownRight" Then b\y = b\y + 10 b\x = b\x + 10 EndIf If b\d$ = "DownLeft" Then b\y = b\y + 10 b\x = b\x - 10 EndIf If b\d$ = "UpRight" Then b\y = b\y - 10 b\x = b\x + 10 EndIf If b\d$ = "UpLeft" Then b\y = b\y - 10 b\x = b\x - 10 EndIf Color 255,0,0 Rect b\x,b\y,5,5 If b\y < 0 Or b\x < 0 Then Delete b Next For a.alien = Each alien If a\e = 1 Then Color 0,255,0 Rect a\x,a\y,25,25 Else Color 129,129,129 Rect a\x,a\y,25,25 End If ;For b.bullet = Each bullet ;Next Next WaitTimer(timer) Flip Wend WaitKey End ---------------------------------- |
| ||
A few things. First, a lot of hardware (can't remember whether it's keyboard or motherboard-specific) do not detect Cursor Up and Cursor Left when pressed together. This is probably what's happening here. Try using a different set of keys and you'll probably find it works. Second, the way you're assigning strings to store the direction of stuff is bizarre. Use Integers with Constants. Finally, that looks like Blitz3D/BlitzPlus code, and this is the Blitzmax forum. |
| ||
My mistake, I'll pay more attention next time. That's interesting about the keyboard issue - I'll keep that in mind. I'm an utter newb at this, so any advice is helpful. I was following along a tutorial on Youtube and decided to branch off and add my own code; hence the bizarre stuff. lol. With a bit of time, maybe I'll be programming like a pro... Give me a few years. :P Thanks again for your help! [EDIT] Now that I think about it though, it is being detected because "UpLeft" is being assigned to move_dir$. The issue is b\d is not being assigned move_dir$ when it's value is "UpLeft". |
| ||
Well, Blitzplus (or whatever that is) isn't really my area of expertise as I haven't used either for a lot of years. But to be brutally honest it does need a re-write so that you're evaluating each key only once. I just wrote this in Blitzmax but I tried to keep it something like what I think should work with BlitzPlus/Blitz3D. It might not work right off with whatever you're using but it should point you in the right direction. Const MOVEUP% = 1 Const MOVEDOWN% = 2 Const MOVELEFT% = 4 Const MOVERIGHT% = 8 Global d% Graphics 640,480 SetBuffer BackBuffer() Repeat Cls d = 0 If KeyDown(200) d = d & MOVEUP EndIf If KeyDown(208) d = d & MOVEDOWN EndIf If KeyDown(203) d = d & MOVELEFT EndIf If KeyDown(205) d = d & MOVERIGHT EndIf drawOutput() Flip Until KeyDown(1) Function drawOutput() Local s$ If d And MOVEUP s = s + "UP " EndIf If d And MOVEDOWN s = s + "DOWN " EndIf If d And MOVELEFT s = s + "LEFT " EndIf If d And MOVERIGHT s = s + "RIGHT" EndIf DrawText s,50,50 End Function |
| ||
Ok, it wasn't that it didn't let me push UP + LEFT at the same time. It just didn't let me push UP + LEFT + SPACE simultaneously. Also - I made my code much cleaner with your example, Gfk. Thank you for your assistance! |
| ||
Ah yeah, up+left+space! That's what I meant! |