(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!
Controlling movement with the mouse
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Controlling movement with the mouse
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:
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.
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
With keyboard, mouse and touchpad for press/release thats a whooping 6 functions to write tho.
obey
Re: Controlling movement with the mouse
Ok, but how do i implement this to my specific project?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: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.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
With keyboard, mouse and touchpad for press/release thats a whooping 6 functions to write tho.
- Attachments
-
- untitledgame.love.zip
- (The Game)
- (12.86 MiB) Downloaded 154 times
Re: Controlling movement with the mouse
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.
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 162 times
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Who is online
Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 2 guests