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...
image mousepressed
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- SimonLarsen
- Party member
- Posts: 100
- Joined: Thu Mar 31, 2011 4:47 pm
- Location: Denmark
- Contact:
Re: image mousepressed
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.)
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 :ultraglee:](./images/smilies/ms-ultra-glee.png)
(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
![:Ultraglee :ultraglee:](./images/smilies/ms-ultra-glee.png)
Re: image mousepressed
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
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!
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
![Surprised :o](./images/smilies/ms-surprise.png)
i guess i will have to start searching the problem is where? well that will be a next question
![Neutral :|](./images/smilies/ms-unplzd.png)
once again thank you for your help!
Re: image mousepressed
lua first edition.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![]()
i guess i will have to start searching the problem is where? well that will be a next questionname of books and where to get them..
once again thank you for your help!
http://www.lua.org/pil/contents.html
luasocket(networking)
http://w3.impa.br/~diego/software/luasocket/
Nasha Löve!
Re: image mousepressed
thank you for sure ill take my time to read it ![Smile :)](./images/smilies/ms-smile.png)
![Smile :)](./images/smilies/ms-smile.png)
- IAsep-TrixI
- Citizen
- Posts: 89
- Joined: Mon Aug 12, 2013 4:22 am
- Location: Philippines,Asia
Re: image mousepressed
You can always try goature, the love2d wiki, or some online love2d tutorial articlespielago wrote:thank you for sure ill take my time to read it
Re: image mousepressed
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
Re: image mousepressed
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
personal page and a raycaster
Re: image mousepressed
and when you need to... ohh wait i dont have any tuts :/Davidobot wrote:And when you get more advanced and let's say, need to save whole tables, you can check out my video.
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests