Drawing things on mouse 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
maxime
Prole
Posts: 2
Joined: Fri Feb 04, 2011 8:37 am

Drawing things on mouse event.

Post 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? :awesome:

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.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Drawing things on mouse event.

Post 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
Last edited by nevon on Fri Feb 04, 2011 8:08 pm, edited 1 time in total.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Drawing things on mouse event.

Post 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)
maxime
Prole
Posts: 2
Joined: Fri Feb 04, 2011 8:37 am

Re: Drawing things on mouse event.

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Drawing things on mouse event.

Post by tentus »

Nevon, you might wanna change those //s into --s, you'll confuse Lua newcomers. :D
Kurosuke needs beta testers
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Drawing things on mouse event.

Post by nevon »

tentus wrote:Nevon, you might wanna change those //s into --s, you'll confuse Lua newcomers. :D
Good catch. I've been writing too much Python and Javascript these days.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing things on mouse event.

Post by Robin »

nevon wrote:Good catch. I've been writing too much Python and Javascript these days.
You do that much integer division then? :P
Help us help you: attach a .love.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Drawing things on mouse event.

Post 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? :P
Hardy har har.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests