How to not go diagonally!?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
stanmarshd
Prole
Posts: 8
Joined: Tue Jul 23, 2013 7:57 am

How to not go diagonally!?

Post 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.
All you need is LÖVE
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: How to not go diagonally!?

Post 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.
User avatar
XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

Re: How to not go diagonally!?

Post 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.
I like to draw and program :)
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: How to not go diagonally!?

Post 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
Your screen is very zoomed in...
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: How to not go diagonally!?

Post 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.
User avatar
XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

Re: How to not go diagonally!?

Post 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:
I like to draw and program :)
User avatar
Omnivore
Prole
Posts: 18
Joined: Fri Jun 28, 2013 12:51 am
Location: West Coast

Re: How to not go diagonally!?

Post 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
Lua lou aye, ah no its, lua louie
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How to not go diagonally!?

Post 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?
User avatar
stanmarshd
Prole
Posts: 8
Joined: Tue Jul 23, 2013 7:57 am

Re: How to not go diagonally!?

Post by stanmarshd »

I got the problem fixed thanks to DaedalusYoung, but before the image would up and right diagonally.
All you need is LÖVE
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 74 guests