Having trouble with Arithmetic.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Having trouble with Arithmetic.
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: The code I am using is as follows: Thank you in advance and if you require any more information, please let me know. Thanks!
Re: Having trouble with Arithmetic.
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.
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.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
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
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.
Last edited by micha on Thu Sep 05, 2013 7:48 am, edited 1 time in total.
Check out my blog on gamedev
Re: Having trouble with Arithmetic.
It now says : attempt to index global 'self' (a nil value). Thanks for the quick reply!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
Re: Having trouble with Arithmetic.
Thank you so much for the help and the quick reply. It now works! Thank you!micha wrote: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.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
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:and have individual update functions in each file (the same goes for drawing and keyboard/mouse handling).Code: Select all
function love.update(dt) if state == 'menu' then menu.update(dt) elseif state == 'level1' then level1.update(dt) end end
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.
Sorry I probably misread something then It's still a bit early.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 9 guests