Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Input!

Author
Message
Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 19th Feb 2004 23:07
Whats the best way to stack your "if" statements to get a good 8 way control on the direction keys?

I have tried doing

if downkey=1 print "down"
if upkey=1 print "up"
if leftkey=1 print "left"
if rightkey=1 print "right"

which works fine, but then trying to get it to do the diagonals is a bit trickier, I tried several different combinations where I got it to print "downleft", "downright" etc but I kept getting it saying "down" and "left" as well. Im just making a logic error I know, but can someone tell me how to do it? So that is says down when I press down, right when I press right and downright when I press both!
IM sure its better as well if I use nested if statements...

GIve me more power!
comando 300
20
Years of Service
User Offline
Joined: 23rd Nov 2003
Location:
Posted: 20th Feb 2004 00:10 Edited at: 20th Feb 2004 00:24
I think if you put this:

if upkey()=1 and leftkey()=1 then print "upleft"


etc...

edit:

or this another way



CURRENT PROJECT: RETRO PAC-MAN
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 20th Feb 2004 00:50
I found that scancodes worked best for combining button presses.

Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 20th Feb 2004 11:51
Commando, if you press the down key and the left key, fine it prints the "down left" part, but it also registers "down" and "left".

Anyway, I got it working by saying

if downkey()=1 and leftkey()=0 and rightkey()=0 print "down"
if downkey()=1 and leftkey()=1 and rightkey()=0 print "downleft"
and so on but it adds up to 8 if statements that look pretty similar and so Im sure the code could be neatened.

Pincho, could you explain the scancode statement a little? I realise the potential, but I cant seem to get the syntax correct...

GIve me more power!
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 20th Feb 2004 13:46
That looks a bit longwinded, personally I'd use something like:

Vertmove=0
Horimove=0
If upkey()=1 then Vertmove=-1
If downkey()=1 then Vertmove=1
If leftkey()=1 then Horimove=-1
If rightkey()=1 then Horimove=1

If you want to use the scancode methods, run this little bit of code and take a note of all the scancodes from the keys you want to use.

Do
cls
Text 0,0,str$(scancode())
sync
Loop

The scancode will tell you the code of the key your pressing - but you don't use that to check the key, use keystate(keycode) to check the status of a key. For instance, say the return key used code 13 - when the return key is being pressed, the scancode would return 13, and keystate(13) would return 1 - but if you press another key at the same time, the scancode would change, but keystate(13) will return 1 as long as your pressing the return key. Try to avoid using Inkey$() for anything.


Van-B


The nature of Monkey was irrepressible!.
Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 20th Feb 2004 14:26
If I use your Vertmove/horimove I still only return 4 values, so to get 8 id have to do
if horimove =-1 and vertmove=-1 then print "downleft"
if horimove =-1 and vertmove=1 then print "upleft"
if horimove =-1 and vertmove=0 then print "left"

and so on through all the permutaions, how is this any quicker then just checking the keys? Basically I want to be able to get my sprite to run in 8 direction and play the correct loop of 8 frames of animation. I got it working, so Ill put up my code later and a link to all my Images, so you all can have a look and help me with some optimisation of the code!

GIve me more power!
PowerFang
20
Years of Service
User Offline
Joined: 6th Feb 2004
Location: Australia (But currently in the USA)
Posted: 21st Feb 2004 00:34
Change the order you check:

x = 0
y = 0

if upkey = 1 then y = -1
if downkey = 1 then y = 1
if leftkey = 1 then x = - 1
if rightkey = 1 then x = 1
if upkey =1 and leftkey = 1 then y = -1 : x = -1
if upkey =1 and rightkey = 1 then y = 1 : x = 1
if downkey =1 and leftkey = 1 then y = 1 : x = -1
if downkey =1 and rightkey = 1 then y = 1 : x = 1

xcurrent = xcurrent + x
ycurrent = ycurrent + y

sprite 1,xcurrent,ycurrent,1

That should do it....

There is one issue with this problem, if you arent fast enough to press both buttons at the same time it will register the buttons seperatly rather then together.

why not just have a button that does the movement diagonally (i.e. the key pad) then it will always register correctly..
Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 21st Feb 2004 01:16
Thanks dude! I got a working setup going, check out my post on the "sprite,playsprite" thread

GIve me more power!

Login to post a reply

Server time is: 2024-05-17 04:31:29
Your offset time is: 2024-05-17 04:31:29