Author Topic: Javascript  (Read 439 times)

Offline capturts

  • Level 8
  • *
  • Posts: 43
  • Reputation: +1/-0
    • View Profile
    • Space Freight
Javascript
« on: October 07, 2011, 09:01:03 AM »
I'm at a loss, I've got 4 buttons on my game: north, south, east and west. They are <input type="button"..... You can click them and they work. How do I use javascript to trigger the same function when I press the arrow keys. I'm using jquery if that helps/hinders. I was originally just going to use the 'accesskey' attribute but had various problems doing that...

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Javascript
« Reply #1 on: October 07, 2011, 12:05:33 PM »
It depends how you're doing it. I assume you're using jQuery to bind "click" events on the buttons to call a named function (perhaps $(".east").bind("click", move("east")) or something to that effect?)

You could do something like:

$("body").bind('keydown', function(e){
    triggerKeydown(e);
});

var triggerKeydown = function(e){
switch(e.keyCode){
  case 38:
    move("north");
    break;
  case 40:
    move("south");
    break;
  case 37:
    move("east");
    break;
  case 39:
    move("west");
    break;
}

}

Check out a working sample: http://jsfiddle.net/3gTPg/

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Javascript
« Reply #2 on: October 07, 2011, 01:00:01 PM »
http://jsfiddle.net/3gTPg/2/ small modification:D

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Javascript
« Reply #3 on: October 07, 2011, 01:28:46 PM »
http://jsfiddle.net/3gTPg/8/ Fixed moving north  :P

Offline capturts

  • Level 8
  • *
  • Posts: 43
  • Reputation: +1/-0
    • View Profile
    • Space Freight
Re: Javascript
« Reply #4 on: October 07, 2011, 06:48:51 PM »
Thanks, I'll have to wait until Sunday to look at this properly :(

Offline Barrikor

  • Level 21
  • *
  • Posts: 248
  • Reputation: +3/-0
    • View Profile
Re: Javascript
« Reply #5 on: October 12, 2011, 05:02:55 PM »
Shouldn't east and west be reversed ?
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline ajacksified

  • Level 5
  • *
  • Posts: 18
  • Reputation: +0/-0
    • View Profile
    • Olivine Labs
Re: Javascript
« Reply #6 on: October 12, 2011, 05:05:24 PM »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal