code errors when drawing rectangle

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
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

code errors when drawing rectangle

Post by Evil_Bartek »

Here is the code:

Code: Select all

function love.mousepressed(x, y, button)
   if button == "l" then
   	    mx = love.mouse.getX()
	    my = love.mouse.getY()
        drawx = mx
		drawy = my
   end
end

function love.draw()
    love.graphics.rectangle("fill",drawx,drawy,40,40)
end

It says that there is a bad argument in the rectangle.
I triple checked to see the errors but I cant see anything wrong!
Help?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: code errors when drawing rectangle

Post by bartbes »

Are drawx and drawy defined before the mouse is clicked the first time.

I'd also like to point out that mx and my aren't needed, the return values could be stored in drawx and drawy directly, and that you don't need to call getX and getY, because an x and y are already passed to it (and more exact too, due to the timing).
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: code errors when drawing rectangle

Post by Evil_Bartek »

So like i should do it in love.update if its possible?
User avatar
zipperipper
Prole
Posts: 45
Joined: Thu Jan 14, 2010 7:59 pm
Location: UK

Re: code errors when drawing rectangle

Post by zipperipper »

Basically

Code: Select all

function love.mousepressed(x, y, button)
	if button == "l" then
		drawx = x
		drawy = y
	end
end

function love.draw()
	if drawx~=nil and drawy~=nil then
		love.graphics.rectangle("fill",drawx,drawy,40,40)
	end
end
This way it wont render the rectangle until you've given a value to drawx and drawy, unless you've declared them in love.load() .
Also use tab to indent code, makes it much neater :3
Error 404, consciousness not found.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: code errors when drawing rectangle

Post by tentus »

Alternative solution, if you only want the rectangle around while you are clicking:

Code: Select all

function love.draw()
   if love.mouse.isDown("l") then
      love.graphics.rectangle("fill", love.mouse.getX(), love.mouse.getY(), 40, 40)
   end
end
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests