Joystick function demo - Updated and complete

Post here your demos and examples with the source files.
Forum rules
Always post the games with a screenshot.
The file must have the ged and data files (complete game source)
Use the forum attachment to post the files.
It is always better to use the first post to put the game files

Joystick function demo - Updated and complete

Postby feral » Wed Jul 09, 2008 5:42 am

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
Last edited by feral on Sun Jul 20, 2008 6:35 am, edited 3 times in total.
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: Joystick function demo

Postby DST » Wed Jul 09, 2008 10:10 am

excellent!
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Joystick function demo

Postby feral » Sat Jul 12, 2008 12:43 am

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
Last edited by feral on Sun Jul 20, 2008 6:32 am, edited 1 time in total.
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: Joystick function demo

Postby feral » Sun Jul 20, 2008 6:13 am

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
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score


Return to Game Demos

Who is online

Users browsing this forum: No registered users and 1 guest

cron