Page 1 of 1

Character Movement

Posted: Sat Apr 01, 2023 10:04 pm
by Todespreis
Hey there! I'm new to LÖVE and Lua and i'm trying to code my first game. And my first obstacle is the movement of a character.
So if i write:

Code: Select all

function love.joystickpressed( joystick, button ) 
                
                if  button==11 then
                    player.x = player.x + 1
                end

            end
            
            function love.joystickpressed( joystick, button )
                
                if  button==2 then
                    player.y = player.y + 1
                end

            end
only the first button works. All the buttons are working, but only the first one. I'm working with nlove, so it's the 0.70 version of the main fork. I think, the program is working correctly and i have some issues with syntax. And is there any better way to code movement, than an if function? Because the if function basically forces the cpu to ask every frame - "Is the button pressed? Is the button pressed? Is the button pressed?" So the cpu is busy all the time with nonsense.

Re: Character Movement

Posted: Sat Apr 01, 2023 10:18 pm
by BrotSagtMist
love.joystickpressed is callback that is activated whenever a button is pressed, it should not be used for movement because one has to press again for every pixel of movement.
Asking if it is pressed every frame is the right way to go and that must be done in the main loop.
Also you are overwriting the assignment:
function love.joystickpressed( joystick, button )
Is supposed to exist only once in your code, if you write it twice, its breaking stuff.

(If that is a an actual user request, this whole post appears to be kinda auto generated nonsense. )

Re: Character Movement

Posted: Sat Apr 01, 2023 10:47 pm
by pgimeno
BrotSagtMist wrote: Sat Apr 01, 2023 10:18 pm (If that is a an actual user request, this whole post appears to be kinda auto generated nonsense. )
Yours or his?

Re: Character Movement

Posted: Sat Apr 01, 2023 10:52 pm
by BrotSagtMist
Booth

Re: Character Movement

Posted: Sun Apr 02, 2023 8:04 pm
by Todespreis
Thank you for the fast reply! Sorry for my bad english, it's not my first language and sometimes i'm not able to say something in a right way.
Well now, after you wrote it, i can actually see, that i'm using 2 fuctions with the same name and yes, it is of course nonsense. So which function would be better? Joystickisdown?

Re: Character Movement

Posted: Sun Apr 02, 2023 8:50 pm
by BrotSagtMist
Two ways:

Code: Select all

love.update=function (dt)
 if js:isDown(1,11 ) then 
    --move
  end
end
Or

Code: Select all

Down={}
function love.joystickpressed( joystick, button )
  Down[button]=true
end
function love.joystickreleased( joystick, button )
  Down[button]=nil
end
love.update=function (dt)
 if Down[11] then 
    --move
  end
end
The later adds an extra table in between, which is helpful to implement one time press actions and include keyboard support and extra joypads without a lot of tinkering.

Re: Character Movement

Posted: Sun Apr 02, 2023 9:23 pm
by Todespreis
Okay, i understand, the function has two values, one for the boolean and one for the button number. But i don't get it working. Thanks for the fast reply, i'll try it tomorrow. Gute Nacht!