Page 1 of 1

[Solved] Game Controlling Question

Posted: Sat Aug 12, 2017 3:58 pm
by ceana
LÖVE has only one mousepressed callback function which is love.mousepressed, so my game's code of controllings with mouse all will put in there? Mouse controllings has varied uses in varied situations, so it's hard to manage it in one function. And the keyboard and touch are also, but at least I could make it work. So what do you do to it?

Because I had never used suchlike framework...it may sound silly :nyu:

Re: [Newbie] Game Controlling Question

Posted: Sat Aug 12, 2017 7:33 pm
by Nelvin
You can change the mousepressed handler if you want to activate a different behaviour during your game.

Here's a most simple example that first defines two different handlers and then activates the first one as a default in the last line. It then just toggles between the two based on the pressed mouse button to show you how it could be done.

Code: Select all

function menuMousePressed( x, y, button, isTouch )
    -- do your menu stuff here
    if button == 1 then
        love.mousepressed = ingameMousePressed
    end
end

function ingameMousePressed( x, y, button, isTouch )
    -- handle ingame mouse here
    if button == 2 then
        love.mousepressed = menuMousePressed
    end
end

love.mousepressed = menuMousePressed

Re: [Newbie] Game Controlling Question

Posted: Sat Aug 12, 2017 10:49 pm
by ivan
Nelvin is on the right track, although a "cleaner" solution might be to just pass all of the input to the current state.
So instead of changing the callback, you react to the input events, based on the current "state" of the game:

Code: Select all

function love.mousepressed(...)
  state.press(...)
end

states = {}
states.menu = {}
function states.menu.press(x,y,b,t)
  -- do menu stuff
end

states.play = {}
function states.play.press(x,y,b,t)
  -- do in-game stuff
end

state = states.menu
Of course you can always avoid callbacks and check if the mouse button is pressed whenever you need, depending on the context.

Re: [Newbie] Game Controlling Question

Posted: Sun Aug 13, 2017 1:43 am
by raidho36
I have an input library that greatly simplifies the whole deal, maybe it will help you.

Re: [Newbie] Game Controlling Question

Posted: Sun Aug 13, 2017 4:25 am
by ceana
Thanks for everyone's help and idea!