How to not go diagonally!?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- stanmarshd
- Prole
- Posts: 8
- Joined: Tue Jul 23, 2013 7:57 am
How to not go diagonally!?
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
Re: How to not go diagonally!?
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.
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!?
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
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
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
Re: How to not go diagonally!?
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 anythingXHH wrote:Code: Select all
if LEFT or RIGHT then UP = false DOWN = false end if UP or DOWN then LEFT = false RIGHT = false end
Your screen is very zoomed in...
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: How to not go diagonally!?
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
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
Re: How to not go diagonally!?
Yea this is what I was thinking, but there must be some better, less tedious way.Then again, the character will stop moving completely when more than one arrow key is pressed, which can be annoying.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
I like to draw and program
Re: How to not go diagonally!?
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
Re: How to not go diagonally!?
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?
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- stanmarshd
- Prole
- Posts: 8
- Joined: Tue Jul 23, 2013 7:57 am
Re: How to not go diagonally!?
I got the problem fixed thanks to DaedalusYoung, but before the image would up and right diagonally.
All you need is LÖVE
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests