LUA attempt to index global anim8 a nil value

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
l0ki77
Prole
Posts: 6
Joined: Tue Apr 18, 2023 7:55 pm

LUA attempt to index global anim8 a nil value

Post by l0ki77 »

Hello I'm a creating a game project for my class and I keep getting the error "attempt to index global anim8 a nil value", which I've tried to figure out for the past couple of hours. I have tried extending anim8 to Player but also get the same error.

Main.lua

function love.load()
Object = require 'libraries/classic'
anim8 = require 'libraries/anim8'
sti = require 'libraries/sti'
gameMap = sti('maps/monMap.lua')
camera = require 'libraries/camera'
tick = require 'libraries/tick'
require 'Player'
require 'Coin'

Coin = Coin()
Player = Player()
cam = camera()

end


function love.update(dt)
Player:update(dt)
Coin:update(dt)
tick.update(dt)

time = time - dt
if time <= 0 then
gameover = true
end


w,h = love.graphics.getDimensions()

if cam.x < w/2 then
cam.x = w/2
end

if cam.y < h/2 then
cam.y = h/2
end

mapW = gameMap.width * gameMap.tilewidth
mapH = gameMap.height * gameMap.tileheight

-- right and bottom edges
if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end



function love.draw()
cam:attach()
gameMap:drawLayer(gameMap.layers["Ground"])
gameMap:drawLayer(gameMap.layers["Tree"])
Player:draw()
Coin:draw()
cam:detach()
end

Player.lua

Player = Object:extend()

function Player:new()
self.image = love.graphics.newImage("sprites/player-sheet.png")
self.x = 100
self.y = 100
self.speed = 300
self.width = love.graphics.getWidth()
self.height = love.graphics.getHeight()
self.grid = anim8.newGrid(12, 18, self.image:getWidth(), self.image:getHeight())

self.animations = {}
self.animations.down = anim8.newAnimation(self.grid('1-4', 1), 0.2)
self.animations.left = anim8.newAnimation(self.grid('1-4', 2), 0.2)
self.animations.right = anim8.newAnimation(self.grid('1-4', 3), 0.2)
self.animations.up = anim8.newAnimation( self.grid('1-4', 4), 0.2)

self.anim = self.animations.down
end

function Player:update(dt)
cam:lookAt(Player.x , Player.y)

isMoving = false

if love.keyboard.isDown('w') then
Player.y = Player.y - Player.speed * dt
self.anim = self.animation.up
isMoving = true
end

if love.keyboard.isDown('s') then
Player.y = Player.y + Player.speed * dt
self.anim = self.animation.down
isMoving = true
end

if love.keyboard.isDown('a') then
Player.x = Player.x - Player.speed * dt
self.anim = self.animation.right
isMoving = true
end

if love.keyboard.isDown('d') then
Player.x = Player.x + Player.speed * dt
self.anim = self.animation.right
isMoving = true
end

if isMoving == false then
Player.anim:gotoFrame(2)
end

self.anim:update(dt)
end

function Player:draw()
Player.anim:draw(Player.image, player.x, player.y, nil, 10)
end
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: LUA attempt to index global anim8 a nil value

Post by MrFariator »

Welcome to the forums, but please, for future reference, use code tags to make your code more readable.

I don't see any immediate problems with how you're using anim8 in what you've posted. Could you provide a .love or a zip of your project, so we could perhaps inspect things further? Also do note that including the whole error traceback could also be beneficial, since it includes the exact line number where the error happens, as well as the code path that led there.
l0ki77
Prole
Posts: 6
Joined: Tue Apr 18, 2023 7:55 pm

Re: LUA attempt to index global anim8 a nil value

Post by l0ki77 »

Problem solved. Thanks
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 1 guest