Page 1 of 1

image mousepressed

Posted: Sat Aug 10, 2013 4:50 am
by pielago
Hi everyone
Can someone please help me
Here it goes I am having problems with love.mousepressed:

I want a simple image to move to where the mouse is pressed in the screen! like if the image is x =100 y=100 and I click any where in the screen.
I want the image to start moving towards where I pressed and stop there.
can someone give me a simple code .I am going crazy trying and nothing...

Re: image mousepressed

Posted: Sat Aug 10, 2013 5:51 am
by SimonLarsen
Here's some quick code that should do the trick. The image will move towards the clicked points with a velocity of 200 units pr. second until the distance is below some threshold, in this case 2. The threshold is used so that the image will not flicker around

(Also, you might not want to store your variables as globals like I did. I simply did so for the sake of simplicity.)

Code: Select all

THRESHOLD = 2
MOVESPEED = 200

function love.load()
	clickx, clicky = 400, 300
	imgx, imgy = 400, 300

	image = love.graphics.newImage("image.png")
end

function love.update(dt)
	-- Calculate distance from image to clicked point
	local dist = math.sqrt((clickx-imgx)^2 + (clicky-imgy)^2)

	-- Move towards point if distance is above threshold
	if dist > THRESHOLD then
		imgx = imgx + (clickx - imgx)/dist * MOVESPEED * dt
		imgy = imgy + (clicky - imgy)/dist * MOVESPEED * dt
	end
end

function love.draw()
	love.graphics.draw(image, imgx, imgy, 0, 1, 1, 16, 16) -- assuming image is 32x32
end

function love.mousepressed(x, y, button)
	if button == "l" then
		clickx = x
		clicky = y
	end
end
I suggest you go read up on "steering behaviors". There are a wide variety of books and articles explaining these in great detail. Check out chapter 3 of Artificial Intelligence for Games. :ultraglee:

Re: image mousepressed

Posted: Sat Aug 10, 2013 7:38 am
by pielago
thank you so much for the info.
To be honest I am super new to love 2D and I can say I didn't know that there are books and articles explaining these in great detail :o
i guess i will have to start searching the problem is where? well that will be a next question :| name of books and where to get them..

once again thank you for your help!

Re: image mousepressed

Posted: Sun Aug 11, 2013 11:40 am
by jjmafiae
pielago wrote:thank you so much for the info.
To be honest I am super new to love 2D and I can say I didn't know that there are books and articles explaining these in great detail :o
i guess i will have to start searching the problem is where? well that will be a next question :| name of books and where to get them..

once again thank you for your help!
lua first edition.
http://www.lua.org/pil/contents.html

luasocket(networking)
http://w3.impa.br/~diego/software/luasocket/

Nasha Löve!

Re: image mousepressed

Posted: Tue Aug 13, 2013 9:38 am
by pielago
thank you for sure ill take my time to read it :)

Re: image mousepressed

Posted: Fri Aug 16, 2013 9:29 pm
by IAsep-TrixI
pielago wrote:thank you for sure ill take my time to read it :)
You can always try goature, the love2d wiki, or some online love2d tutorial articles

Re: image mousepressed

Posted: Sun Aug 18, 2013 10:40 pm
by Eamonn
Or you could check out my Lua and LÖVE tutorials.... they might not be amazing, but they're my best work(which might not be that good, but I thought it'd share it anyway).

Re: image mousepressed

Posted: Mon Aug 19, 2013 4:44 am
by Davidobot
Eamonn wrote:Or you could check out my Lua and LÖVE tutorials.... they might not be amazing, but they're my best work(which might not be that good, but I thought it'd share it anyway).
And when you get more advanced and let's say, need to save whole tables, you can check out my video.

Re: image mousepressed

Posted: Mon Aug 19, 2013 11:29 am
by jjmafiae
Davidobot wrote:
Eamonn wrote:Or you could check out my Lua and LÖVE tutorials.... they might not be amazing, but they're my best work(which might not be that good, but I thought it'd share it anyway).
And when you get more advanced and let's say, need to save whole tables, you can check out my video.
and when you need to... ohh wait i dont have any tuts :/

maybe in the future when im good at making multiplayer and better at using lövelymün then i may make an multiplayer tut because its damn hard to make multiplayer games.

but if you fall then ten more lövers shall take your place.