Author Topic: Adding WASD control in tandem with arrow keys  (Read 260 times)

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Adding WASD control in tandem with arrow keys
« on: July 29, 2017, 07:56:54 am »
I'm working on making a game with Solarus and I want the WASD keys to be usable for moving the character alongside the arrow keys, kinda like how you can use the demo mouse_control script and the arrow keys at the same time. Is this possible to do? I've tried making a script off of the mouse_control one but it isn't seeming to work. It doesn't help that d is a pause key, apparently. If anyone can point out where I messed up, I'd love to know. Thanks!

Code: Lua
  1. require("scripts/multi_events")
  2.  
  3.  
  4. local function initialize_wasd_control_features(game)
  5.  
  6.   local wasd_control = {}
  7.  
  8.   local is_key_pushed = false
  9.   print("wasd control startup")
  10.  
  11.   -- Movement of the hero.
  12.   local directions_pressed = {
  13.       right = false,
  14.       up = false,
  15.       left = false,
  16.       down = false
  17.   }
  18.  
  19.   function wasd_control:on_key_pressed(key)
  20.     is_key_pushed = true
  21.    
  22.   end
  23.  
  24.   function wasd_control:on_mouse_released(keu)
  25.  
  26.     is_key_pushed = false
  27.     pressed_key = key
  28.   end
  29.  
  30.   function wasd_control:on_update()
  31.  
  32.     local map = game:get_map()
  33.     if map == nil then
  34.       return
  35.     end
  36.  
  37.     if is_key_pushed then
  38.  
  39.       pressed_right = sol.input.is_key_pressed("d")
  40.       pressed_up = sol.input.is_key_pressed("w")
  41.       pressed_left = sol.input.is_key_pressed("a")
  42.       pressed_down = sol.input.is_key_pressed("s")
  43.  
  44.       self:update_direction("right", pressed_right)
  45.       self:update_direction("up", pressed_up)
  46.       self:update_direction("left", pressed_left)
  47.       self:update_direction("down", pressed_down)
  48.     else
  49.  
  50.       for direction, _ in pairs(directions_pressed) do
  51.         self:stop_direction(direction)
  52.       end
  53.     end
  54.     function wasd_control:start_direction(direction)
  55.  
  56.       if not directions_pressed[direction] then
  57.         directions_pressed[direction] = true
  58.         game:simulate_command_pressed(direction)
  59.       end
  60.     end
  61.  
  62.     function wasd_control:stop_direction(direction)
  63.  
  64.      if directions_pressed[direction] then
  65.         directions_pressed[direction] = false
  66.         game:simulate_command_released(direction)
  67.       end
  68.     end
  69.   end
  70. end
  71. local game_meta = sol.main.get_metatable("game")
  72. game_meta:register_event("on_started", initialize_wasd_control_features)
  73. return true

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Adding WASD control in tandem with arrow keys
« Reply #1 on: July 29, 2017, 01:02:43 pm »
It should be simpler by using game:on_key_pressed/released and calling game:simulate_command_pressed/released() from there.

To avoid the conflict with the D key, just assign the pause command to another key than D.

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Adding WASD control in tandem with arrow keys
« Reply #2 on: July 30, 2017, 01:49:33 am »
Much better. Thank you!!!

Yeury

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding WASD control in tandem with arrow keys
« Reply #3 on: September 25, 2017, 01:34:52 am »
It should be simpler by using game:on_key_pressed/released and calling game:simulate_command_pressed/released() from there.

To avoid the conflict with the D key, just assign the pause command to another key than D.
Hey is there an exact way to reassign D? because I tried assigning P to pause and D to move right but D pauses AND moves right

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Adding WASD control in tandem with arrow keys
« Reply #4 on: September 25, 2017, 09:25:17 am »
Normally if you assign P to pause then the previous key (D) is removed.