Character Movement

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
Todespreis
Prole
Posts: 24
Joined: Sat Apr 01, 2023 9:30 pm

Character Movement

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Character Movement

Post 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. )
obey
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Character Movement

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Character Movement

Post by BrotSagtMist »

Booth
obey
Todespreis
Prole
Posts: 24
Joined: Sat Apr 01, 2023 9:30 pm

Re: Character Movement

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Character Movement

Post 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.
obey
Todespreis
Prole
Posts: 24
Joined: Sat Apr 01, 2023 9:30 pm

Re: Character Movement

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests