This is my first game. Ever. This is also my first forum post. With that out of the way, I would like to discuss my issue.
Since this is my first game, alot of the code I use it based off of tutorials. So...yeah.
I've used this code before, for testing, etc. When I tried to run it this time, I got this error:
Player.lua:22: attempted to perform arithmetic on field "speed" (a nil value)
Traceback:
player.lua:22: in function player_move
main.lua:10: in function update
[C:]in function xpcall
Here's my code:
main.lua
require "player"
function love.load()
end
function love.update(dt)
player_move()
end
function love.draw()
player_draw()
end
player.lua
player = {}
player.X = 400
player.Y = 300
player.Speed = 5
player.Health = 100
player.Damage = 10
player = love.graphics.newImage("Graphics/Main dude/player.jpg")
function player_draw()
love.graphics.draw(player,player.X,player.Y)
end
function player_move()
if love.keyboard.isDown("left") then
player.X = player.X - player.Speed*dt
elseif love.keyboard.isDown("right") then
player.X = player.X + player.Speed*dt
end
end
player = {}
player.X = 400
player.Y = 300
player.Speed = 5
player.Health = 100
player.Damage = 10
player = love.graphics.newImage("Graphics/Main dude/player.jpg")
function player_draw()
love.graphics.draw(player,player.X,player.Y)
end
function player_move()
if love.keyboard.isDown("left") then
player.X = player.X - player.Speed*dt
elseif love.keyboard.isDown("right") then
player.X = player.X + player.Speed*dt
end
end
If anyone can give me any help, tips, whatever. Whatever works. Everything is appreciated.
Problem with movement.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Problem with movement.
Hi and welcome to the forum,
this line
means that you are trying to use a field called "speed" and this does not exist. This is probably, because it should be "Speed" (with a capital letter).
One advice for future posts: Put your code into code-tags. That way it gets formatted and is easier to read. Just click on the Code button when you write a post and insert the code in between.
this line
Code: Select all
Player.lua:22: attempted to perform arithmetic on field "speed" (a nil value)
One advice for future posts: Put your code into code-tags. That way it gets formatted and is easier to read. Just click on the Code button when you write a post and insert the code in between.
Check out my blog on gamedev
Re: Problem with movement.
micha wrote: this linemeans that you are trying to use a field called "speed" and this does not exist. This is probably, because it should be "Speed" (with a capital letter).Code: Select all
Player.lua:22: attempted to perform arithmetic on field "speed" (a nil value)
One advice for future posts: Put your code into code-tags. That way it gets formatted and is easier to read. Just click on the Code button when you write a post and insert the code in between.
I wrote all of that down. Rather than copy and pasting it, because... I don't think you can do that. I created a tiny project just so I could test out the same code, and it seemed to work. It worked every other time on every other project. What might be the problem with that?
Also, I shall. Thank you for your time.
Re: Problem with movement.
Eh, is this dead or something?
Re: Problem with movement.
I think nobody really had an answer; I'm not sure I really understand your question. Can you elaborate a bit more and what you want to know?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Problem with movement.
What Plu said. Please read the forum rules. In particular, upload a .love of your game if you want us to help you with a problem, and explain precisely what that problem is.
Help us help you: attach a .love.
Re: Problem with movement.
Yus, thanks. I shall do that.Robin wrote:What Plu said. Please read the forum rules. In particular, upload a .love of your game if you want us to help you with a problem, and explain precisely what that problem is.
Re: Problem with movement.
I can. I'm not sure what you want me to elaborate on specifically, so I'll give you more detail. The code above, which I've used before, seems to not work. I have no idea why. It's exactly the same as it's always been. I don't really know where I went wrong, so I can't really say more than that.Plu wrote:I think nobody really had an answer; I'm not sure I really understand your question. Can you elaborate a bit more and what you want to know?
Thinking about it, maybe I should just delete the code and write it all back up. There isn't much, so I'm sure it'll be easy.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Problem with movement.
That seems to be a problem. You override the player table with a newImage. It should be fixed if you change that line to something like:Efbrefus wrote:Code: Select all
player = {} --(...) player = love.graphics.newImage("Graphics/Main dude/player.jpg")
Code: Select all
player.image = love.graphics.newImage("Graphics/Main dude/player.jpg")
Re: Problem with movement.
Holy Christ, I never noticed that. Lemme try that out.
...
...
Marry me.
...
...
Marry me.
Who is online
Users browsing this forum: Bing [Bot] and 0 guests