Change image/drawing position to the mouse position every frame

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
masterjohn12
Prole
Posts: 1
Joined: Sat Nov 11, 2017 1:59 pm

Change image/drawing position to the mouse position every frame

Post by masterjohn12 »

I have the following code and I dont know what how to do this

Code: Select all

function love.load()
    image = love.graphics.newImage("ricardo.jpg")
    w, h = love.graphics.getDimensions()
    wi, hi = image:getDimensions()
end

function love.update(dt)
    -- Change image position accordingly to the mouse position
end
-- Draw a coloured rectangle.
function love.draw()
    love.graphics.draw(image, w/2, h/2, 0, 0.5,0.5,wi/2,hi/2)
end
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Change image/drawing position to the mouse position every frame

Post by zorg »

Hi and welcome to the forums!

First, you need to get the mouse position, look at the wiki for love.mouse functions.
Then you need two variables to hold the x and y coordinates for your image.
You need to update those variables each frame, as the mouse moves.
Finally, use those variables in love.draw; you only need the first 3 parameters; the image, and the x and y position though.

If you do want to center your image too, then you do require the wi/2 and hi/2 parameters you had before.
Last edited by zorg on Sat Nov 11, 2017 7:51 pm, edited 1 time in total.
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.
RednibCoding
Prole
Posts: 8
Joined: Fri Nov 10, 2017 7:31 pm

Re: Change image/drawing position to the mouse position every frame

Post by RednibCoding »

I've postet something similar on github lately for coding tilemaps:
https://github.com/RednibCoding/Love2D_ ... r/main.lua

Code: Select all

-- in love.update --
-- getting mouse position
	mouseX, mouseY = love.mouse.getPosition()


-- in love.draw --
	-- print the mouseX mouseY position next to the cursor 
	love.graphics.print(mouseX.." | "..mouseY, mouseX, mouseY)
Instead of printing text, just draw your image at mouseX, mouseY
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Change image/drawing position to the mouse position every frame

Post by Tjakka5 »

Editing your code slightly:

Code: Select all

function love.load()
    image = love.graphics.newImage("ricardo.jpg")
    x, y = 0, 0
    wi, hi = image:getDimensions()
end

function love.update(dt)
    -- Change image position accordingly to the mouse position
    x, y = love.mouse.getPosition()
end
-- Draw a coloured rectangle.
function love.draw()
    love.graphics.draw(image, x, y, 0, 0.5,0.5,wi/2,hi/2)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests