[Help] How to move object to mouse clicked loaction
Posted: Fri Sep 07, 2012 6:11 am
About Me
Hi the name is Pat I have almost no coding experience. I am in Afghanistan board out of my minde lol So i figure why not put my time to good use. I have watched all the sock monkey tutorials and some of the love tutorials on the wiki.
The question
I was working on Love Tutorial:Hamster Ball. When I wondered how I could get it so that when I left click the mouse on a location he will move at a set speed to that location. I did a few searches on the forums and did not see what I was looking fore. Im sure its out there I may have just missed out. So if there is a good tutorial on this could you please point me in the right direction.
Edit 9/7/2012 This issues has been solved by Roland_Yonaba
Updated code of working mouse moments
Also take a look at https://love2d.org/wiki/love.mousepressed
Hi the name is Pat I have almost no coding experience. I am in Afghanistan board out of my minde lol So i figure why not put my time to good use. I have watched all the sock monkey tutorials and some of the love tutorials on the wiki.
The question
I was working on Love Tutorial:Hamster Ball. When I wondered how I could get it so that when I left click the mouse on a location he will move at a set speed to that location. I did a few searches on the forums and did not see what I was looking fore. Im sure its out there I may have just missed out. So if there is a good tutorial on this could you please point me in the right direction.
Edit 9/7/2012 This issues has been solved by Roland_Yonaba
Updated code of working mouse moments
Code: Select all
function love.load()
medium = love.graphics.newFont(45)
hamster = love.graphics.newImage("Images/hamster.png") --- Define variable for .png and file location of .png
x = 0
y = 0
destX = 0 --- Must Declare a start location for destX and destY
destY = 0
speed = 100 --- The movement Speed of the .png
end
function love.mousepressed(x,y,button) --- This is the code to for the mouse buttons
if button == 'l' then --- This is for the left mouse button
destX, destY = x,y
end
end
function love.update(dt)
if x ~= destX then --- The math for the movement
x = x + (dt*speed)*((destX-x)/math.abs(destX-x))
end
if y ~= destY then
y = y + (dt*speed)*((destY-y)/math.abs(destY-y))
end
end
function love.draw()
love.graphics.draw(hamster, x, y) --- The variable defined for the .png start location
end