[Solved] Game Controlling Question

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
ceana
Prole
Posts: 9
Joined: Fri Aug 11, 2017 5:08 pm
Location: A place where English is taught

[Solved] Game Controlling Question

Post 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:
Last edited by ceana on Sun Aug 13, 2017 4:52 am, edited 1 time in total.
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: [Newbie] Game Controlling Question

Post 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
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [Newbie] Game Controlling Question

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: [Newbie] Game Controlling Question

Post by raidho36 »

I have an input library that greatly simplifies the whole deal, maybe it will help you.
ceana
Prole
Posts: 9
Joined: Fri Aug 11, 2017 5:08 pm
Location: A place where English is taught

Re: [Newbie] Game Controlling Question

Post by ceana »

Thanks for everyone's help and idea!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 53 guests