LUA attempt to index global anim8 a nil value
Posted: Fri Apr 28, 2023 11:26 pm
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
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