How to make an action dependent upon love.mouse.isDown

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
pizzahut91
Prole
Posts: 3
Joined: Sun Jul 31, 2016 7:11 am

How to make an action dependent upon love.mouse.isDown

Post by pizzahut91 »

I'm kind of a newbie, but right now I'm tinkering around and trying to get an image to move across the screen in a direction dependent on the side of the screen the user clicks on.

So basically the image moves left or right when the left or right side of the screen is clicked, but only while the mouse is down.

What I have right now is this:

Code: Select all

function love.mousepressed(x, y, button, istouch)
	while love.mouse.isDown(1)==true do
		if button == 1 then -- Versions prior to 0.10.0 use the MouseConstant 'l'
			if x < 598 then
				velocity = -2
			elseif x > 598 then
				velocity = 2
			else
				repeat
					velocity = velocity - 1
				until velocity <= 0
				velocity = 0
			end
		end
   end
end
So basically in the love.mousepressed function I've tried to have a while loop that checks if the mouse is pressed, then depending on which half of the screen (based off of my phone's screen resolution because this stemmed out of touch controls) is clicked, increases or decreases the velocity. The else at the end is just trying to get the moving object to slow down gradually rather than instantly but the number needs adjustment.

Right now it won't do anything if you click anywhere in the window, not sure what I need to do to fix this.
User avatar
Semeon
Prole
Posts: 36
Joined: Tue Jul 12, 2016 1:35 pm

Re: How to make an action dependent upon love.mouse.isDown

Post by Semeon »

Hey, first things first it is better to instead of figuring out yourself you screen half resolution, let LOVE find it for you exactly on any screen
the code is:

Code: Select all

love.graphics.getWidth() * 0.5  -- One Way
love.graphics.getWidth() / 2  -- Second Way
Both work the same.

Now onto your problem I can't seem to figure out any problems I would recommend you give us the .love file so I can see the code in a "larger scale" and try to figure ou the problem.
pizzahut91
Prole
Posts: 3
Joined: Sun Jul 31, 2016 7:11 am

Re: How to make an action dependent upon love.mouse.isDown

Post by pizzahut91 »

Sorry about that - I had to remake the code because I was an idiot and started trailing off doing some other thing in the same file but I got everything back in order and this .love file should more or less be the same.

Another worrying thing is that the program straight up stops responding when left on its own for too long, but maybe I've just written some horrifying feedback loop or something neat sounding like that.
Attachments
Basic Mouse Movement.love
Had to reupload because original was zipped wrong
(17.57 KiB) Downloaded 153 times
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make an action dependent upon love.mouse.isDown

Post by zorg »

You should only detect things with the mousepressed (and mousereleased) callbacks.

Set some variables in there, then periodically check in love.update, instead of writing an endless loop inside the above callbacks, since that hands your application.

Also, you use love.mouse.isDown in love.update, since the mousepressed callback will be called only when a click did happen.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: How to make an action dependent upon love.mouse.isDown

Post by MadByte »

First, welcome to the forum!

You've got one main problem here. love.mousepressed is called only once, so it doesn't help to build a while/repeat construct there.
Try to use love.update instead.

Some more advices:
#1: When using velocities in LÖVE, try to use dt to make sure your velocity isn't framerate dependent.
#2: try to avoid fixed variables like x=598. Better use something like love.graphics.getWidth()/2 or similar.

I've build an example for you to look at.
Moving on mouseclick thingy.love
If you got further questions, feel free to ask.
pizzahut91
Prole
Posts: 3
Joined: Sun Jul 31, 2016 7:11 am

Re: How to make an action dependent upon love.mouse.isDown

Post by pizzahut91 »

MadByte wrote:First, welcome to the forum!

You've got one main problem here. love.mousepressed is called only once, so it doesn't help to build a while/repeat construct there.
Try to use love.update instead.

Some more advices:
#1: When using velocities in LÖVE, try to use dt to make sure your velocity isn't framerate dependent.
#2: try to avoid fixed variables like x=598. Better use something like love.graphics.getWidth()/2 or similar.

I've build an example for you to look at.
Moving on mouseclick thingy.love
If you got further questions, feel free to ask.
Thanks for the code reference! Definitely a lot nicer looking than what I've been making...

What I want the "game" (if you could call it that at this point) to do, though, is make the object move at a set velocity only while a side of the screen is clicked/touched.

I'm not entirely sure how I would modify your program to do that, though, it's using a lot of functions and methods I'm not entirely familiar with yet, but I think I get the gist of how everything is working.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: How to make an action dependent upon love.mouse.isDown

Post by MadByte »

pizzahut91 wrote: What I want the "game" (if you could call it that at this point) to do, though, is make the object move at a set velocity only while a side of the screen is clicked/touched.
That's pretty easy to do and also would reduce the amount of code needed because it wouldn't make sense to use love.mousepressed since it just detect one click and doesn't update. To expand on my previous code I would suggest to change the update function to use love.mouse.isDown and remove the direction variable part as well as the mousepressed part.

Updated example:
Edit: updated the *.love because I left the unused object:mousepressed() function in the code.
MouseClickMovement.love
(920 Bytes) Downloaded 145 times
pizzahut91 wrote: I'm not entirely sure how I would modify your program to do that, though, it's using a lot of functions and methods I'm not entirely familiar with yet, but I think I get the gist of how everything is working.
No worry, it's just because I organise things differently. :crazy:
You can use the wiki to find out what these functions do.
For the rest take a look at the Lua PIL.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest