Jump to content
MakeWebGames

PHP keyboard input (movement)


ShadyCoco

Recommended Posts

Hey,

I'm using a tile based game engine writen in PHP,mysql, java (small amount) and I really want to add keyboard movement along side pressing buttons (on screen) to move.

Any ideas on what language I should use to do it?

You wanna use javascript and just capture which key is hit then run the function.

Link to comment
Share on other sites

I', using this but it don't seem to be collecting it.

function getkey(e)
{
if (window.event)
  return window.event.keyCode;
else if (e)
  return e.which;
else
  return null;
}

function redir(key)
{
       var rkey = getkey(key);

       if(rkey == 119)
       {
               document.location.href="index.php?x=200&y=100";
       }

       if(rkey == 115)
       {
               document.location.href="index.php?x=200&y=200";
       }

       if(rkey == 97)
       {
               document.location.href="index.php?x=150&y=150";
       }

       if(rkey == 100)
       {
               document.location.href="index.php?x=250&y=150";
       }
}
Link to comment
Share on other sites

If your using the jQuery library, you may have better luck using the jKey plugin

Then you do something like (portion taken from their samples page):

[js]

$(document).jKey('up, left, down, right, up+right, up+left, down+right, down+left', function (key) {

//Current location is y = 2 x = 2

if (key == 'up') {

player.location = 'n:3,2'; // North, y =3, x =2

} else if (key == 'down') {

player.location = 's:1,2'; // South y = 1, x = 2

} else if (key == 'left') {

player.location = 'w:2,1'; // West y = 2, x = 1

} else if (key == 'right') {

player.location = 'e:2,3'; // East y = 2, x = 3

} else if (key == 'up+right') {

player.location = 'ne:3,3'; // N'East y=3, x = 3

}

//So on and so forth.

});

[/js]

for example.

Urgh didn't see your previous code Shady, would have saved me thinking of an appropriate value for the updated location. T.T

Edited by Djkanna
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...