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

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.
pighead10
Prole
Posts: 6
Joined: Fri Oct 29, 2010 5:32 pm

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

Post 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)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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
...
When I write def I mean function.
pighead10
Prole
Posts: 6
Joined: Fri Oct 29, 2010 5:32 pm

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

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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. ;)
Help us help you: attach a .love.
User avatar
Luca
Prole
Posts: 7
Joined: Tue Oct 19, 2010 12:32 pm
Location: England
Contact:

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

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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
Help us help you: attach a .love.
pighead10
Prole
Posts: 6
Joined: Fri Oct 29, 2010 5:32 pm

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

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Well, they're not nil if you assign a value (other than nil) to them first, yes. :P
Help us help you: attach a .love.
pighead10
Prole
Posts: 6
Joined: Fri Oct 29, 2010 5:32 pm

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

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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. ;)
Help us help you: attach a .love.
Post Reply

Who is online

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