Page 1 of 1

How to not go diagonally!?

Posted: Tue Jul 23, 2013 8:16 am
by stanmarshd
My game uses WSAD controls. Whenever you hold, say, w and d, you go diagonally up and right. I want this to go away. Please help me.

Re: How to not go diagonally!?

Posted: Tue Jul 23, 2013 8:59 am
by Plu
Either before you move check if multiple keys are pressed and then don't move, or put the moves in an if-elseif structure so that only one keydown is registered at most.

The former is a bit more code and means your character stands still if you press both "w" and "a", the second is a bit easier but it means that if someone pushes all the buttons at once, they'll still move in one direction; whichever is highest up in the chain.

If you need more help, just shout.

Re: How to not go diagonally!?

Posted: Tue Jul 23, 2013 12:33 pm
by XHH

Code: Select all

if LEFT or RIGHT then
     UP = false
     DOWN = false
end
if UP or DOWN then
     LEFT = false
     RIGHT = false
end

if LEFT then x = x - 5
if RIGHT then x = x + 5
if UP then y = y - 5
if DOWN then y = y + 5
This is how I would approach it. However it will favor left/right over up/down because it's for loop is first. And, of course this isn't actual code. You need to add the keyboard key checks in the beginning.

EDIT:
To eliminate the favoring part, try something along these lines...

Code: Select all

if (LEFT or RIGHT) and not (UP or DOWN) then
     if LEFT then x = x - 5
     if RIGHT then x = x + 5
end
if (UP or DOWN) and not (LEFT or RIGHT) then
     if UP then y = y - 5
     if DOWN then y = y + 5
end
Of course, ANOTHER PROBLEM with this one. If more than one key is pressed the player stop completely.

My last idea for this is to get the LAST key pressed and just use that with an if statement for each seperate key.

Hope this helps in some way.

Re: How to not go diagonally!?

Posted: Tue Jul 23, 2013 12:44 pm
by mickeyjm
XHH wrote:

Code: Select all

if LEFT or RIGHT then
     UP = false
     DOWN = false
end
if UP or DOWN then
     LEFT = false
     RIGHT = false
end
Those two if statements are redundant because no matter what by the time you reach the second if statement there will only be (UP or DOWN) pressed or (Left or RIGHT) pressed but not both, meaning the other if statement will never change anything

Re: How to not go diagonally!?

Posted: Tue Jul 23, 2013 4:14 pm
by DaedalusYoung

Code: Select all

if up then y = y -1
elseif down then y = y + 1
elseif left then x = x - 1
elseif right then x = x + 1
end
This way, the if...then loop will stop at the first true statement. That does mean that there is a bias, if a player holds both up and right, the character will go up, because that's the first check in the loop.

To fix this, you can do something like this:

Code: Select all

if up and not left and not right and not down then y = y - 1
elseif down and not up and not left and not right then y = y + 1
-- etcetera
end
Then again, the character will stop moving completely when more than one arrow key is pressed, which can be annoying.

Re: How to not go diagonally!?

Posted: Tue Jul 23, 2013 11:53 pm
by XHH

Code: Select all

if up and not left and not right and not down then y = y - 1
elseif down and not up and not left and not right then y = y + 1
-- etcetera
end
Then again, the character will stop moving completely when more than one arrow key is pressed, which can be annoying.
Yea this is what I was thinking, but there must be some better, less tedious way. :ultrashocked:

Re: How to not go diagonally!?

Posted: Wed Jul 24, 2013 1:34 am
by Omnivore

Code: Select all

local MOVE_KEYS, move_key = {w = 'w', a = 'a', s = 's', d = 'd'}

local function on_keypressed(key)
  move_key = MOVE_KEYS.key or move_key
  -- whatever else
end

local function on_keyreleased(key)
  if key == move_key then move_key = nil end
  -- more whatevers
end

local function on_update()
  if move_key then
    -- do the move
  end
  -- and even more whatevers
end

Re: How to not go diagonally!?

Posted: Wed Jul 24, 2013 5:21 am
by T-Bone
Stanmarchd, it would help if you told us what you want to happen if you press for example up and right at the same time, Should you move up, right or perhaps not at all?

Re: How to not go diagonally!?

Posted: Wed Jul 24, 2013 11:59 am
by stanmarshd
I got the problem fixed thanks to DaedalusYoung, but before the image would up and right diagonally.