ouse clicked object

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
BueDev
Prole
Posts: 3
Joined: Sat May 31, 2014 5:38 pm

ouse clicked object

Post by BueDev »

First off, i want to say sorry if there is already a thred on this (I know there is some where) but I've spent 30 minuets already looking for it. So here is my question, I want to simulate a command (if statement) when the mosue has clicked on a object. How would I do this?
I am not the bue you are looking for...Or am I...Don't think so....Hold on let me look at the file...Nope, I'm a talking cheese burgre, move along please - And that's why I hate Apples ^-^
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: ouse clicked object

Post by Germanunkol »

You probably didn't find anything in your search because there is so many ways to do this...

The basic, simplest way I can currently think of is this:
In love.mousepressed, check if the mouse was inside the region which is also the region where the object currently is.
If so, call the function.

Here's a quick example. This should make the rectangle change color every time it's clicked (not tested)

Code: Select all


local object = { x = 10, y = 20, width = 100, height = 50, color = 0 }

function love.mousepressed( x, y, button )
    -- Check if the click is inside the object:
    if x > object.x and y > object.y and x < object.x + object.width and y < object.y + object.height then
        switchObjectColor()
    end
end

function switchObjectColor()
    if object.color == 0 then
        object.color = 1
    else
        object.color = 0
    end
end

function love.draw()
    if object.color = 0 then
        love.graphics.setColor( 255, 0, 0, 255 )
    else
        love.graphics.setColor( 0, 255, 0, 255 )
    end
    love.graphics.rectangle( "fill", object.x, object.y, object.width, object.height)
end
Edit:
P.S. Welcome to the forums!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Chron
Prole
Posts: 2
Joined: Wed Dec 25, 2013 6:55 pm

Re: ouse clicked object

Post by Chron »

Basically, you treat it similarly to how you would treat collisions in gameplay.

Assuming the object you want to check for collision with the mouse is a rectangle, for example a button, here's a function you could use to check for collision between a point (the mouse position) and a rectangle (the button):

Code: Select all

function pointRectangleCollide(pointX, pointY, rectangleX, rectangleY, rectangleWidth, rectangleHeight)
	left = rectangleX
	right = left + rectangleWidth
	top = rectangleY
	bottom = top + rectangleHeight

	return
		left <= pointX and
		top <= pointY and
		pointX <= right and
		pointY <= bottom
end
What this function does is check if the point is
  • - To the right of the left line of the rectangle,
    - Below the top line of the rectangle,
    - To the left of the right line of the rectangle,
    - And above the bottom line of the rectangle.
If all is true, then the point is inside the rectangle, and therefore colliding. Only in this case, the function above returns true.

Other shapes, such as rotated rectangles, are more complicated - I suggest you to read up 2D collisions on the internet if you want to learn more.
BueDev
Prole
Posts: 3
Joined: Sat May 31, 2014 5:38 pm

Re: ouse clicked object

Post by BueDev »

thank you so much for your responses! also, I am very sorry about the errors in the post, I was typing quickly, and shortly found out that my keyboard became unresponsive ^^ still, no excuse for ill grammar. Thank you for your warm welcome to!
I am not the bue you are looking for...Or am I...Don't think so....Hold on let me look at the file...Nope, I'm a talking cheese burgre, move along please - And that's why I hate Apples ^-^
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests