Request sample code to detect image onclick event

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
User avatar
weilies
Prole
Posts: 28
Joined: Sat Oct 09, 2010 6:16 am

Request sample code to detect image onclick event

Post by weilies »

Greeting guru,

How can love engine detect a mouse clicked on my character sprite ?

From the doc provided, I only saw code to capture input from keyboard and mouse
How can I tell my program to increase my score when my mouse clicked on enemy image object?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Request sample code to detect image onclick event

Post by Robin »

Well, then you're half way there. You now only have to compare the location of the mouse to the location you draw the Image (technically, they're not really sprites).

In pseudocode it'll be something like:

Code: Select all

function love.mousepressed(x, y)
    if x >= draw_x and x <= draw_x + width and y >= draw_y and y <= draw_y + height then
        score = score + 1
    end
end
Help us help you: attach a .love.
User avatar
weilies
Prole
Posts: 28
Joined: Sat Oct 09, 2010 6:16 am

Re: Request sample code to detect image onclick event

Post by weilies »

os, there is no

logic like

Code: Select all

my_enemey.onClick {
     score = score + 1
}
from your suggested code, imagine it's a polygon, then using ur code might end up trigger the increase score even click outside the image
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Request sample code to detect image onclick event

Post by Robin »

weilies wrote:os, there is no

logic like

Code: Select all

my_enemey.onClick {
     score = score + 1
}
Nope. That's not even valid Lua.
weilies wrote:from your suggested code, imagine it's a polygon, then using ur code might end up trigger the increase score even click outside the image
Well, most of the time a rectangular or circular bounding box is precise enough. if you really want pixel-perfect clicking, you'd need to use a Shape, set its position to the same location as the image and then run myshape:testPoint(x, y) on it. If it returns true, you clicked on the image. If you want to see it used in an actual unfinished game, you could take a look at http://github.com/gvx/Conq-rer.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], pericles and 7 guests