Page 1 of 1

[SOLVED]Help: drawing consecutively with a "brush"

Posted: Fri Sep 19, 2014 1:27 pm
by nice
Hello boys and girls!
I'm currently making a drawing program (a MS paint clone) and I can draw by clicking on the screen but that's a bit of a pain so I want to be able to draw by holding down the mouse button and drag the mouse cursor around on my screen, so here's the code:

Code: Select all

 function love.load()

squares = {}
xPos = 0
yPos = 0


--Draws canvas
	canvas = love.graphics.newCanvas(800, 480)

end

function love.update( dt )

local newPosition = {}

	if love.mouse.isDown( "l" ) then
		newPosition.x = x
		newPosition.y = y
		table.insert( squares, newPosition )
	end


	local newPosition = {}
	if love.mouse.isDown("l" ) then
		newPosition.x = x 
		newPosition.y = y 
	end
end


-- Draws All The Graphics --
function love.draw()

-- Prints X And Y Position On The Upper Left Screen
local x, y = love.mouse.getPosition()
	love.graphics.print("X:" ..x, 10, 10)
	love.graphics.print("Y:" ..y, 10, 40)

-- Set The Background To White
	love.graphics.setBackgroundColor(255, 255, 255)

-- UI: Banner Grey
	love.graphics.setColor(195 , 195, 195)
	love.graphics.rectangle("fill", 0, 480, 800, 120)

-- Draws A 16x16 Square That Follows The Mouse
	love.graphics.rectangle("line", x, y, 16, 16)

-- Draws A 16x16 Square "brush"

for key, square in ipairs( squares ) do
  	love.graphics.rectangle( "fill", square.x, square.y, 16, 16 ) 
end

end
And this is the part (so far) that makes the "game" crash:

Code: Select all

 love.graphics.rectangle( "fill", square.x, square.y, 16, 16 ) 
So can someone help me? I have been trying to figure this one out for a while and I can't manage to see the problem..

Re: Help: drawing consecutively with a "brush"

Posted: Fri Sep 19, 2014 1:34 pm
by Jepcats
You need to add the line

Code: Select all

local x, y = love.mouse.getPosition()
in love.update(). At the moment the x and y you're referring to don't exist.

Re: Help: drawing consecutively with a "brush"

Posted: Fri Sep 19, 2014 1:38 pm
by nice
Thanks! I would give you a kiss if I could! :awesome: