image mousepressed

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

image mousepressed

Post 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...
User avatar
SimonLarsen
Party member
Posts: 100
Joined: Thu Mar 31, 2011 4:47 pm
Location: Denmark
Contact:

Re: image mousepressed

Post 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:
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: image mousepressed

Post 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!
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: image mousepressed

Post 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!
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: image mousepressed

Post by pielago »

thank you for sure ill take my time to read it :)
User avatar
IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

Re: image mousepressed

Post 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
An agent of the free

check out the game I'm doing with Jkash!
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: image mousepressed

Post 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).
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: image mousepressed

Post 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.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: image mousepressed

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest