Mouse input question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
artschoolcamouflage
Prole
Posts: 4
Joined: Sat Jun 13, 2020 8:16 pm

Mouse input question

Post by artschoolcamouflage »

I'm fairly new to LOVE 2d, lua and coding in general. I've made some progress over the last few weeks, but the road ahead is a long one. What I'm stuck on at the moment is implementing a basic (I think) drag-and-drop code into my game (https://ebens.me/post/mouse-dragging-in-love2d) as a starting point. This example is for having LOVE draw an image and allow the mouse to grab and move the image. What I can't figure out is how to apply the code to an object that uses rxi's "Classic" code. I've put in Ebens' code to my object file and I'm not getting any errors (finally!), but I'm also not getting any input. Anything obvious jump out at anyone? Advice would be much appreciated. My code is below.

Code: Select all

RedBlocks = Object:extend(Object)

function RedBlocks:new(x, y, width, height)
    self.image = love.graphics.newImage("red_block.png")
    self.x = x
    self.y = y
    self.width = 60
    self.height = 60
    self.dragging = { active = false, diffX = 0, diffY = 0}
end

function RedBlocks:update(dt)
    if self.dragging.active then
        self.x = love.mouse.getX() - self.dragging.diffX
        self.y = love.mouse.getY() - self.dragging.diffY
    end
end

function RedBlocks:mousepressed(x, y, button)
    if button == 1
    and x > self.x and x < self.x + self.width
    and y > self.y and y < self.y + self.height
    then
        self.dragging.active = true
        self.dragging.diffX = x - self.x
        self.dragging.diffY = y - self.y
    end
end

function RedBlocks:mousereleased(x, y, button)
    if button == 1 then
        self.dragging.active = false
    end
end

function RedBlocks:draw()
    love.graphics.draw(self.image, self.x, self.y, 0, 2, 2)
end 
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Mouse input question

Post by ReFreezed »

You'll still need to use LÖVE's event handlers (i.e. love.mousepressed) to get the input, and then propagate the information to your objects.

Code: Select all

function love.load()
	block = RedBlocks(0, 0, 50, 50)
end
function love.update(dt)
	block:update(dt)
end
function love.mousepressed(x, y, button)
	block:mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
	block:mousereleased(x, y, button)
end
function love.draw()
	block:draw()
end
Edit: I also see you're setting the width and height of the object to 60 no matter what the image size is, and you're also scaling the image by 2 when drawing it. This all may affect where the "hit box" for the object appears to be.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
artschoolcamouflage
Prole
Posts: 4
Joined: Sat Jun 13, 2020 8:16 pm

Re: Mouse input question

Post by artschoolcamouflage »

That worked! Thank you.

At this point the scaling isn't an issue, but I think you are probably right. I'll address that at a later time. For now, I can move on to my next roadblock, which I'm excited to find out what that will be :)
Post Reply

Who is online

Users browsing this forum: No registered users and 66 guests