Page 1 of 1
Drawing things on mouse event.
Posted: Fri Feb 04, 2011 11:33 am
by maxime
Hi,
I'm new here; I had a question about how to draw things.
I would like to draw an image when i click with my mouse.
And I want the image to be drawn when I click somewhere else without deleting the old one.
How can I do this?
Code: Select all
function love.load()
img = {
stone = love.graphics.newImage("stone.png"),
}
function love.draw(dt)
love.graphics.draw(img.stone,a-9,b-9)
end
function love.mousepressed(x, y, button)
x, y = love.mouse.getPosition( )
pressed = 1
print(pressed)
a=x
b=y
end
I tried this but, of course, the image disappears when I click somewhere else.
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 11:41 am
by nevon
You'd have to create a table where you store all images that you want drawn. Something like this (untested):
Code: Select all
function love.load()
img = {
stone = love.graphics.newImage("stone.png")
}
objects = {}
end
function love.draw()
--A for loop that draws all the objects in our objects table.
--The _ is the index of the current object in the loop. obj is the value of the current object in the loop.
--In this case it's the table we're inserting in the mousepressed callback.
for _,obj in ipairs(objects) do
love.graphics.draw(obj.image, obj.x, obj.y)
end
end
function love.mousepressed(x,y, button)
if button == 'l' then
--Insert a table into our objects table.
--The new table contains a reference to the image, and the coordinates where to draw it.
table.insert(objects, {image = img.stone, x=x, y=y})
end
end
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 3:34 pm
by thelinx
maxime wrote:
Code: Select all
love.graphics.draw(img.stone,a-9,b-9)
You may want to utilise
love.graphics.draws "origin" parameters.
Code: Select all
-- love.graphics.draw( drawable, x, y, orientation (0), scale-x (1), scale-y (scale-x), origin-x (0), origin-y(0))
love.graphics.draw(img.stone, a, b, 0, 1, 1, 9, 9)
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 4:20 pm
by maxime
I tried with the table.
I used a 2d table and it works great!
this is the main piece of code in the draw function:
Code: Select all
for i=0,40 do
for j=0,30 do
if t[i][j].z == 1 then
love.graphics.draw(img.ston1, t[i][j].x, t[i][j].y)
elseif t[i][j].z == 2 then
love.graphics.draw(img.stone2, t[i][j].x, t[i][j].y)
end
end
end
I'm probably going to use the "origin" for others purpose.
Thanks a lot, have a good day.
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 6:19 pm
by tentus
Nevon, you might wanna change those //s into --s, you'll confuse Lua newcomers.
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 8:08 pm
by nevon
tentus wrote:Nevon, you might wanna change those //s into --s, you'll confuse Lua newcomers.
Good catch. I've been writing too much Python and Javascript these days.
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 10:46 pm
by Robin
nevon wrote:Good catch. I've been writing too much Python and Javascript these days.
You do that much integer division then?
Re: Drawing things on mouse event.
Posted: Fri Feb 04, 2011 11:18 pm
by nevon
Robin wrote:nevon wrote:Good catch. I've been writing too much Python and Javascript these days.
You do that much integer division then?
Hardy har har.