Need Beginners Help with Nil Value error

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
New2Coding
Prole
Posts: 1
Joined: Sun Feb 18, 2024 1:35 pm

Need Beginners Help with Nil Value error

Post by New2Coding »

Here I am just trying to follow a guide on how to make my character sprite split up, but I'm getting an error:
Error

main.lua:9: attempt to index field 'spriteSheet' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:9: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'


This is the code below:function love.load()
anim8 = require 'libraries/anim8'
love.graphics.setDefaultFilter("nearest", "nearest")
player = {}
player.x = 48
player.y = 72
player.speed = 5
player.sprite = love.graphics.newImage('sprites/player-sheet.png')
player.grid = anim8.newGrid( 12, 18, player.spriteSheet:getWidth(), player.spriteSheet:getHeight() )
player.animations = {}
player.animations.down = anim8.newAnimation( player.grid('1-4', 1), 0.2 )
player.animations.left = anim8.newAnimation( player.grid('1-4', 2), 0.2 )
player.animations.right = anim8.newAnimation( player.grid('1-4', 3), 0.2 )
player.animations.up = anim8.newAnimation( player.grid('1-4', 4), 0.2 )

player.anim = player.animations.left

background = love.graphics.newImage('sprites/backgroundtest.png')

end

function love.update(dt)
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
end

if love.keyboard.isDown("left") then
player.x = player.x - player.speed
end

if love.keyboard.isDown("down") then
player.y = player.y + player.speed
end

if love.keyboard.isDown("up") then
player.y = player.y - player.speed
end
player.animations.down:update(dt)
end

function love.draw()
love.graphics.draw(background, 0, 0)
player.anim:draw(player.spriteSheet, player.x, player.y)
end



I'm simple not sure at all why I can't seem to get this to work. I'm following the guide to the T's being crossed and the I's being dotted but i can't seem to find my mistake.....
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: Need Beginners Help with Nil Value error

Post by RNavega »

Hi. Read the error message, it's worth gold:

Code: Select all

main.lua:9: attempt to index field 'spriteSheet' (a nil value)
It says the error is on your "main.lua" script, on line #9, which happens to be this:

Code: Select all

player.grid = anim8.newGrid( 12, 18, player.spriteSheet:getWidth(), player.spriteSheet:getHeight() )
The error says: "you're trying to use the value contained in 'player.spriteSheet', but 'player.spriteSheet' is nil (nonexistent)."
You have "player.sprite" in there, on line #8. Is that what you meant?
Post Reply

Who is online

Users browsing this forum: Google [Bot], pgimeno, Yolwoocle and 58 guests