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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

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

Post 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..
Attachments
Screen Shot 2014-09-19 at 15.21.03.png
Screen Shot 2014-09-19 at 15.21.03.png (16.97 KiB) Viewed 2138 times
Last edited by nice on Fri Sep 19, 2014 1:38 pm, edited 1 time in total.
:awesome: Have a good day! :ultraglee:
Jepcats
Prole
Posts: 8
Joined: Thu Sep 18, 2014 3:19 pm

Re: Help: drawing consecutively with a "brush"

Post 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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: drawing consecutively with a "brush"

Post by nice »

Thanks! I would give you a kiss if I could! :awesome:
:awesome: Have a good day! :ultraglee:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest