Cursor Key Problems

Blitz3D Forums/Blitz3D Programming/Cursor Key Problems

Nexic(Posted 2004) [#1]
Hey. My current project allows the user to play using the cursor keys. However the player cannot move diagonally when pressing two keys together (up and left, down and right etc)

At the moment I am using something like this

if KeyDown(leftkey) then MovePlayerY(-10)
if KeyUp(leftkey) then MovePlayerY(10)
if KeyLeft(leftkey) then MovePlayerX(-10)
if KeyRight(leftkey) then MovePlayerX(10)

Anyone got any ideas?


Sledge(Posted 2004) [#2]
"KeyUp"?!



No.



EDIT: (This isn't a Max thing is it? Say not and we'll proceed from there.)


Perturbatio(Posted 2004) [#3]
um... are you supposed to be using the same key for all four directions?


Perturbatio(Posted 2004) [#4]
This works fine for me:




Sledge(Posted 2004) [#5]
Ditto, but with the key codes referenced as variables (so you can easily let the player redefine 'em)

Graphics 640,480,0,2
SetBuffer BackBuffer()

KLEFT=203
KRIGHT=205
KUP=200
KDOWN=208

px=160
py=120

While Not KeyDown(1)

Text px,py,"O"

If KeyDown(KLEFT) Then px=px-1
If KeyDown(KRIGHT) Then px=px+1
If KeyDown(KUP) Then py=py-1
If KeyDown(KDOWN) Then py=py+1
Flip True
Cls
Wend


I see you went for the lower case "o" there Pert; I, myself, prefer the capitalised variant. Look, I wrote a ruddy example and I'm posting it! :D


Nexic(Posted 2004) [#6]
Lol, my code does actually use the right key codes, I just forgot them in my pseudo code :)

Seems very odd, maybe its my keyboard thats at fault.


Sledge(Posted 2004) [#7]
The code you posted is faulty - KeyDown() refers to a key (any key, specified in the parenthesis) being pressed down, not specifically to the "down arrow" key. There are no such commands as KeyUp, KeyRight and KeyLeft - a slight shift in your thinking, and you'll have it.


Perturbatio(Posted 2004) [#8]
Seems very odd, maybe its my keyboard thats at fault.

Do the examples above work for you? My Movement Lite edition and Sledges Movement XP?


Nexic(Posted 2004) [#9]
Yes they work for me.

Ill say it again, leftkey is meant to be a variable with the correct key code in it.

Must be doing something wrong :S


PetBom(Posted 2004) [#10]
@Nexic
Sledge told you what you are doing wrong:

There are no such commands as KeyUp, KeyRight and KeyLeft

There is one function to check if any key is pressed, and that is:
KeyDown()
You will need four variables containing the coresponding scancodes to use with the Keydown() function to determine if the key is pressed:
keyleft=203
keyright=205
keyup=200
keydown=208
That would give you a code that looks like this:
If KeyDown(keyleft) Then px=px-1    ;This checks if Cursor Left is pressed
If KeyDown(keyright) Then px=px+1   ;This checks if Cursor Right is pressed
If KeyDown(keyup) Then py=py-1      ;This checks if Cursor Up is pressed
If KeyDown(keydown) Then py=py+1    ;This checks if Cursor Down is pressed
Your code actually tries to do the opposite thing, i.e. you have four functions (that does not exist) using one variable

Did that clarify things?

//PetBom