Page 1 of 1

Having more than one image on the screen

Posted: Fri Nov 25, 2011 3:53 pm
by iLuvLove
For some odd reason, I am having trouble displaying more than one image on the screen. Attached is the .love file of my project. Does anyone know what is wrong? Why does it only display one image, and not both?

Re: Having more than one image on the screen

Posted: Fri Nov 25, 2011 4:44 pm
by Boolsheet
You're using global variables instead of the values from the Rock instance.

Code: Select all

function Rock:getX()
	return x
end
This will return the value of the global variable x.
You probably want to use self.x here to get the x from the Rock instance.

Code: Select all

function Rock:getX()
	return self.x
end
There are a few more places where self is missing.

Re: Having more than one image on the screen

Posted: Sat Nov 26, 2011 6:06 pm
by iLuvLove
Oh... Thanks. I'm new to lua, so I don't quite have the whole global/local thing figured out yet.

Re: Having more than one image on the screen

Posted: Wed Nov 30, 2011 11:04 pm
by LuaWeaver
Local is just to the function. If I have a function with a local variable, I can reuse that variable later, out of the function.

Code: Select all

function code()
local localCode={"This is a table with a string"}
end
print(localCode[1]) --Results in error.
A global can be used ANYWHERE.

Code: Select all

function code()
return {"This may or may not be a variable when this is called"}
end
globalCode=code()
print(globalCode[1]) --Prints "This may or may not be a variable when this is called"

Re: Having more than one image on the screen

Posted: Fri Dec 09, 2011 2:57 pm
by iLuvLove
Thanks. I've figured out how the local/global stuff works now though.