Page 1 of 2

How do you get the x and y positions of an image?

Posted: Fri Oct 29, 2010 5:43 pm
by pighead10
Hey, I've been using Lua for ages and I'm quite advanced, but I've just started using LOVE2D and seem to have got stuck at the first hurdle. :ehem:

I thought you could use 'imgx' and 'imgy', but that doesn't seem to be working with my newest script. I swear imgx and imgy worked for my last script, but...

Here's the code, in case you need it.

Code: Select all

local image

function love.load()
	image = love.graphics.newImage("Images/smiley.png")
	local f = love.graphics.newFont(1)
	love.graphics.setFont(f)
	love.graphics.setBackgroundColor(50,50,255)
end

function love.draw()
	love.graphics.draw(image,imgx,imgy)
end

function love.update(dt)
	local h,w = image:getHeight(),image:getWidth()
	local onimage = h - imgy <= mouse:getY() and w - imgx <= mouse:getX() and mouse.isDown("w") or false --ERROR LINE
	if onimage then
		imgx = mouse:getX() + (w/2)
		imgy = mouse:getY() + (h/2)
	end
end
The output (or whatever you call it with LOVE2D) is:
Attempt to perform arithmetic on global 'imgy' (a nil value)

Re: How do you get the x and y positions of an image?

Posted: Fri Oct 29, 2010 6:03 pm
by kikito
On the first call to love.update, imgx and imgx are not set. Their value is, literally, nil.

You should figure out what should be their initial value ( [0,0]? the center of the screen?) and assign it at the begining:

Code: Select all

local image
local imgx = 100 -- for example
local imgy = 100 -- for example
...

Re: How do you get the x and y positions of an image?

Posted: Fri Oct 29, 2010 6:26 pm
by pighead10
On the first call to love.update, imgx and imgx are not set. Their value is, literally, nil.
Oh, I didn't know that. Thanks!

Re: How do you get the x and y positions of an image?

Posted: Fri Oct 29, 2010 7:09 pm
by Robin
Exactly. There is nothing magic about imgx and imgy, it is the same for every variable, which you should know if you are experienced with Lua. ;)

Re: How do you get the x and y positions of an image?

Posted: Sat Oct 30, 2010 1:47 am
by Luca
I think it's because you are doing this:

Code: Select all

local onimage = h - imgy <= mouse:getY() and w - imgx <= mouse:getX() and mouse.isDown("w") or false --ERROR LINE
if onimage then
Try:

Code: Select all

if h - imgy <= mouse:getY() and w - imgx <= mouse:getX() and mouse.isDown("w") then

Re: How do you get the x and y positions of an image?

Posted: Sat Oct 30, 2010 8:34 am
by Robin
Luca wrote:I think it's because you are doing this:

Code: Select all

local onimage = h - imgy <= mouse:getY() and w - imgx <= mouse:getX() and mouse.isDown("w") or false --ERROR LINE
if onimage then
Try:

Code: Select all

if h - imgy <= mouse:getY() and w - imgx <= mouse:getX() and mouse.isDown("w") then
No it isn't. It's good that you try to help, but as you can see, the issue was already fixed (nil is nil, no matter how you formulate things). OP: please don't get confused by this. :P

Re: How do you get the x and y positions of an image?

Posted: Sat Oct 30, 2010 8:24 pm
by pighead10
Exactly. There is nothing magic about imgx and imgy
It wasn't nil when I used it in something other than love.update.

Re: How do you get the x and y positions of an image?

Posted: Sat Oct 30, 2010 8:46 pm
by Robin
Well, they're not nil if you assign a value (other than nil) to them first, yes. :P

Re: How do you get the x and y positions of an image?

Posted: Sun Oct 31, 2010 4:14 pm
by pighead10
Robin wrote:Well, they're not nil if you assign a value (other than nil) to them first, yes. :P

Code: Select all

local image

function love.load()
	image = love.graphics.newImage("Images/smiley.png")
	love.graphics.setBackgroundColor(50,50,255)
end

function love.draw()
	love.graphics.draw(image,imgx,imgy)
end
That doesn't error.

Re: How do you get the x and y positions of an image?

Posted: Sun Oct 31, 2010 5:10 pm
by Robin
pighead10 wrote:That doesn't error.
That's because that's different: apparently, the love.graphics.draw() function accepts nil as 0. However, you cannot say nil + 4 or something like that: arithmetic with nil has no meaning, thus it errors. Rule number one of math in programming languages: you can't do math with undefined variables. ;)