Controlling movement with the mouse

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
Josh.G
Prole
Posts: 13
Joined: Thu Jul 29, 2021 6:16 pm

Controlling movement with the mouse

Post 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:
User avatar
BrotSagtMist
Citizen
Posts: 62
Joined: Fri Aug 06, 2021 10:30 pm

Re: Controlling movement with the mouse

Post 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.
obey
Josh.G
Prole
Posts: 13
Joined: Thu Jul 29, 2021 6:16 pm

Re: Controlling movement with the mouse

Post 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?
Attachments
untitledgame.love.zip
(The Game)
(12.86 MiB) Downloaded 68 times
milon
Party member
Posts: 203
Joined: Thu Jan 18, 2018 9:14 pm

Re: Controlling movement with the mouse

Post 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.
Attachments
InputDemo.love
(1.58 KiB) Downloaded 48 times
Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests