Page 1 of 1

Controlling movement with the mouse

Posted: Mon Aug 30, 2021 7:39 pm
by Josh.G
(Im new to lua)
I was planning to format a game Im making in a way that would be suitable for mobile, since mouse clicks work as "presses" on a phone I would like to know how to controll movements (in this case left and right) with mouse clicks especially in a way that would be mobile friendly.
Thanks! :awesome:

Re: Controlling movement with the mouse

Posted: Mon Aug 30, 2021 8:10 pm
by BrotSagtMist
Touch screens and mouses are usually two different pairs of devices.

Anyway for this case, first write your game as you normally would for a keyboard.
then use the touchsreen callback to redirect these presses into the keyboard logic:

Code: Select all

X,Y= love.graphics.getDimensions()--get screen size
function love.touchpressed( id, x, y, dx, dy, pressure )
 if x<X/2 then --if the click was below half of the screens X size its a left
  love.event.push( "keypressed","left","left" )
 else 
  love.event.push( "keypressed","right","right" )-- else a right
 end
end
Of course this will only _push_ once, for a continuous walk you can channel every input into a global keys table and then add/delete entries on press/release events.
With keyboard, mouse and touchpad for press/release thats a whooping 6 functions to write tho.

Re: Controlling movement with the mouse

Posted: Thu Sep 02, 2021 12:39 am
by Josh.G
BrotSagtMist wrote: Mon Aug 30, 2021 8:10 pm Touch screens and mouses are usually two different pairs of devices.

Anyway for this case, first write your game as you normally would for a keyboard.
then use the touchsreen callback to redirect these presses into the keyboard logic:

Code: Select all

X,Y= love.graphics.getDimensions()--get screen size
function love.touchpressed( id, x, y, dx, dy, pressure )
 if x<X/2 then --if the click was below half of the screens X size its a left
  love.event.push( "keypressed","left","left" )
 else 
  love.event.push( "keypressed","right","right" )-- else a right
 end
end
Of course this will only _push_ once, for a continuous walk you can channel every input into a global keys table and then add/delete entries on press/release events.
With keyboard, mouse and touchpad for press/release thats a whooping 6 functions to write tho.
Ok, but how do i implement this to my specific project?

Re: Controlling movement with the mouse

Posted: Tue Sep 14, 2021 2:46 pm
by milon
Here's my take on it. I personally don't use love.update() to handle input - I use Love's own callback functions for that. Feel free to adapt my demo to your style or whatever. I didn't include mouse support (you have to track both clicks and movement separately), but swipe movement is supported (tested on my Android).

Controls are arrow keys to move, or WASD if you prefer letters. It should also be compatible with other non-US layouts, but I have no way to test that myself. ESC to quit.

This probably doesn't need to be said, but this is hereby released as free code - use in anyway you like with no restrictions, no attribution required.