Discord
Login
Community
DARK THEME

[W A S D] Movement Help

So I'm Making A Game That I Want To Rely On [W A S D] Movement For The Players, But I Currently Have [Mouse]/[Trackpad] Movement. The Problem Is I'm An Idiot When It Comes To JavaScript And I Don't Know How To Switch Over To [W A S D] Movement Or What To Replace To Do So. If Someone Could Help It Would Be Greatly Appreciated! Code To Fix Shown Below!

Sincerely, Voiddshade

  players = object end
  connection = new ServerConnection()
  x = 0
  y = 0
end

update = function()
  if mouse.pressed and (mouse.x != x or mouse.y != y) then
    x = mouse.x
    y = mouse.y
    connection.send(object
      x = mouse.x
      y = mouse.y
    end)
  end

Why not allow for both at the same time? It does not really matter in my view if you check for both at the same time. Just some pseudo code here to demonstate handling of the cursor / WASD keys to move a sprite:

update = function()
  // Move the sprite according to cursor or WASD inputs
  mysprite.x+=cursorXdir()
  mysprite.y+=cursorYdir()
end



// Returns -1, 0, or 1 depending on the cursors left/right or A/D keys
cursorXdir=function()-keyboard.LEFT+keyboard.RIGHT end

// Returns -1, 0, or 1 depending on the cursors down/up or S/W keys
cursorYdir=function()-keyboard.DOWN+keyboard.UP end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community