Page 1 of 1

Joystick function demo - Updated and complete

PostPosted: Wed Jul 09, 2008 5:42 am
by feral
new better improved and "GE" styled version - see last post. please let me know if it works or not..

the attached example uses the cursor keys to emulate joystick controls using a function

EDIT - old versions/notes removed .. now redundant

Re: Joystick function demo

PostPosted: Wed Jul 09, 2008 10:10 am
by DST
excellent!

Re: Joystick function demo

PostPosted: Sat Jul 12, 2008 12:43 am
by feral
feral wrote:note2: currently stops if both left and right are pressed and then up or down are pressed as well.. I will fix that soon


actually i have continued to test this problem in order to fix it..

It appears tho, that it is a problem either with GE or How the cursor keys are scanned by the keyboard.

The down key and any combination of left, right, or left AND right all work OK

it is when left AND right are pressed with the UP key, that it doesn't work (only one combination out of all possible.. so not too bad...)... can others please confirm this ? it should work.. and may just be only my keyboard and or windows 95...

EDITED

Re: Joystick function demo

PostPosted: Sun Jul 20, 2008 6:13 am
by feral
New Improved version - previous version have been deleted except for relevant notes:

What it now does, is work exactly the same as GetKeyState except it works only with the cursor keys (can be remapped), but, it does all the redundant checking for you, regarding having two keys pressed down at the same time, which was the last key pressed, AND can return a keystate combination (diagonals) and should automatically stop all moonwalking if used right.


How to use
Copy the code at the end of this post (or cut and paste from the demo file) into a Global Script then ignore it 8)

In the draw event of your player
Initialize it by calling it...
GetJoyState(var); where var is = 4 or = 8 movement style joystick

Then use it the same way GetKeyState is used, except use the words joy/JOY in place of key/KEY

for example
Code: Select all
GetJoyState(4);

if (joy[JOY_RIGHT]==1)
{
    x=x+2;
}


GetJoyState(4); this gives the option of 4 joystick motions
possible values are JOY_RIGHT, JOY_LEFT, JOY_UP, JOY_DOWN

where up/down and right/left are combined eg: if right key is pressed and up key is pressed then both JOY_RIGHT and JOY_UP will ==1

this usually means the x and y speeds will combined into a diagonal motion, old school joystick motion

however

GetJoyState(8);
possible values are JOY_RIGHT, JOY_LEFT, JOY_UP, JOY_DOWN
plus
JOY_UP_LEFT,JOY_UP_RIGHT,JOY_DOWN_LEFT,JOY_DOWN_RIGHT

where up/right type combinations can be dealt with separately
eg: when up/right keys are pressed JOY_UP_RIGHT will be ==1 but JOY_UP and JOY_RIGHT will ==0;

this allows separate actions on diagonal combinations such as jumping/kicking etc or different diagonal speeds

to test, download the example file
currently in joystick 8 mode
use cursor keys to move
then change the function in player draw event to (4)
the JOY_UP_RIGHT type definitions will then be ignored

demo
getjoy.zip
example file
(9.81 KiB) Downloaded 188 times



CODE to place in a GLOBAL script

Code: Select all
int joy[255];
#define JOY_RIGHT 2
#define JOY_LEFT 4
#define JOY_UP 8
#define JOY_DOWN 16
#define JOY_UP_LEFT 12
#define JOY_UP_RIGHT 10
#define JOY_DOWN_LEFT 20
#define JOY_DOWN_RIGHT 18
// following defines are not needed but are defined to allow mismatching
#define JOY_LEFT_UP 12
#define JOY_RIGHT_UP 10
#define JOY_LEFT_DOWN 20
#define JOY_RIGHT_DOWN 18


void GetJoyState(int type)
{

char *key = GetKeyState(); //Get entire keyboard state
char combox=0;
char comboy=0;
char lastXkey;
char lastYkey;

joy[2]=joy[4]=joy[8]=joy[16]=joy[10]=joy[12]=joy[18]=joy[20]=0;

if(key[KEY_RIGHT] == 1 && key[KEY_LEFT]==0) //Test if right key is pressed
{
joy[JOY_RIGHT]=1;
joy[JOY_LEFT]=0;
lastXkey=4;
}

if(key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 0) //Test if left key is pressed
{
joy[JOY_RIGHT]=0;
joy[JOY_LEFT]=1;
lastXkey=2;
}

if(key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 1) //Test if left key is pressed
{
joy[JOY_RIGHT]=0;
joy[JOY_LEFT]=0;
joy[lastXkey]=1;
}

if(key[KEY_UP] == 1 && key[KEY_DOWN]==0) //Test if right key is pressed
{
joy[JOY_UP]=1;
joy[JOY_DOWN]=0;
lastYkey=16;
}

if(key[KEY_DOWN] == 1 && key[KEY_UP] == 0) //Test if left key is pressed
{
joy[JOY_UP]=0;
joy[JOY_DOWN]=1;
lastYkey=8;
}

if(key[KEY_UP] == 1 && key[KEY_DOWN] == 1) //Test if left key is pressed
{
joy[JOY_UP]=0;
joy[JOY_DOWN]=0;
joy[lastYkey]=1;
}

if (type==8)
{
   if (joy[JOY_UP]==1 && joy[JOY_RIGHT]==1)
   {
   joy[JOY_UP]=0;
   joy[JOY_RIGHT]=0;
   joy[JOY_UP_RIGHT]=1;
   }
   if (joy[JOY_UP]==1 && joy[JOY_LEFT]==1)
   {
   joy[JOY_UP]=0;
   joy[JOY_LEFT]=0;
   joy[JOY_UP_LEFT]=1;
   }

   if (joy[JOY_DOWN]==1 && joy[JOY_RIGHT]==1)
   {
   joy[JOY_DOWN]=0;
   joy[JOY_RIGHT]=0;
   joy[JOY_DOWN_RIGHT]=1;
   }
   if (joy[JOY_DOWN]==1 && joy[JOY_LEFT]==1)
   {
   joy[JOY_DOWN]=0;
   joy[JOY_LEFT]=0;
   joy[JOY_DOWN_LEFT]=1;
   }
} //end if type


} //end function