Page 1 of 1
Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 6:37 am
by HJW012
Hello. First off, I'd like to say that I am quite new to Love2D and Lua in general so bare with me.
Recently, I have been trying to make a simple game with a friend of mine. I know that I should work on some easier things before jumping the gun and making a game but I decided to jump right in. The first thing I had was a simple level where a player could walk left and right and jump. It worked fine until I made the main menu. It now shows the following error:
- KMbyX9A.png (17.55 KiB) Viewed 354 times
The code I am using is as follows:
- zFqpVQd.png (71.92 KiB) Viewed 354 times
Thank you in advance and if you require any more information, please let me know. Thanks!
Re: Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 7:21 am
by Plu
If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
Re: Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 7:47 am
by micha
Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
While it is generally a good idea to use the self-shortcut, this is not the problem here. The table player can be accessed with its name.
The problem in the code is that the function player.load() is never called. Make sure that when you change to the game-state that you call player.load.
In your current implementation you seem to have a gamestate and every time you change the game state you redefine love.draw() and love.update() and also love.load(). Note that love.load() is really only called once in the very beginning. If you change state you have to call it manually.
Besides that I suggest you do not redefine the love-main functions. It might look cleaner in the code, because you save one if-condition. But debugging can get more tedious because the function name is not unique anymore (debugging is still possible, because you get the file and line number).
So better do an in-condition in the main loop:
Code: Select all
function love.update(dt)
if state == 'menu' then
menu.update(dt)
elseif state == 'level1' then
level1.update(dt)
end
end
and have individual update functions in each file (the same goes for drawing and keyboard/mouse handling).
Oh, and I suggest next time you use the code-tags instead of pasting an image of the code. That way it is easier for us to copy the code and help you.
Re: Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 7:48 am
by HJW012
Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
It now says : attempt to index global 'self' (a nil value). Thanks for the quick reply!
Re: Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 7:58 am
by HJW012
micha wrote:Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
While it is generally a good idea to use the self-shortcut, this is not the problem here. The table player can be accessed with its name.
The problem in the code is that the function player.load() is never called. Make sure that when you change to the game-state that you call player.load.
In your current implementation you seem to have a gamestate and every time you change the game state you redefine love.draw() and love.update() and also love.load(). Note that love.load() is really only called once in the very beginning. If you change state you have to call it manually.
Besides that I suggest you do not redefine the love-main functions. It might look cleaner in the code, because you save one if-condition. But debugging can get more tedious because the function name is not unique anymore (debugging is still possible, because you get the file and line number).
So better do an in-condition in the main loop:
Code: Select all
function love.update(dt)
if state == 'menu' then
menu.update(dt)
elseif state == 'level1' then
level1.update(dt)
end
end
and have individual update functions in each file (the same goes for drawing and keyboard/mouse handling).
Oh, and I suggest next time you use the code-tags instead of pasting an image of the code. That way it is easier for us to copy the code and help you.
Thank you so much for the help and the quick reply. It now works! Thank you!
Re: Having trouble with Arithmetic.
Posted: Thu Sep 05, 2013 8:06 am
by Plu
Sorry I probably misread something then
It's still a bit early.