Problem with movement.

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.
Post Reply
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Problem with movement.

Post by Efbrefus »

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.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Problem with movement.

Post by micha »

Hi and welcome to the forum,

this line

Code: Select all

Player.lua:22: attempted to perform arithmetic on field "speed" (a nil value)
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.
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Problem with movement.

Post by Efbrefus »

micha wrote: this line

Code: Select all

Player.lua:22: attempted to perform arithmetic on field "speed" (a nil value)
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.

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.
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Problem with movement.

Post by Efbrefus »

Eh, is this dead or something?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Problem with movement.

Post by Plu »

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

Re: Problem with movement.

Post by Robin »

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.
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Problem with movement.

Post by Efbrefus »

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.
Yus, thanks. I shall do that.
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Problem with movement.

Post by Efbrefus »

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?
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.

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.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Problem with movement.

Post by DaedalusYoung »

Efbrefus wrote:

Code: Select all

player = {}

--(...)

player = love.graphics.newImage("Graphics/Main dude/player.jpg")
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:

Code: Select all

player.image =  love.graphics.newImage("Graphics/Main dude/player.jpg")
And then of course in your player_draw function, change the name of the image variable to player.image as well.
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Problem with movement.

Post by Efbrefus »

Holy Christ, I never noticed that. Lemme try that out.

...
...
Marry me.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests