Brief introduction: I've been learning to code for the last few months and have done a few tutorials and made a few games, I'm at the stage where I'm trying to create my own game (barebones at the moment and using parts of tutorials, goature among others) and it was working until I put in a menu now it comes up with "states/openworld/main.lua:156 attempt to index global 'player' (a nil value)". It's really confusing that it worked without the menu but nothing is wrong with the menu. I thought I solved this problem but it's come back and I'm really stuck, my best guess is something is wrong with function player:update but it seems ok to me. Any advice would be greatly appreciated. Full Code Below.
Code: Select all
local AdvTiledLoader = require("AdvTiledLoader.Loader")
require("camera")
function love.load()
love.graphics.setBackgroundColor( 220, 220, 255 )
AdvTiledLoader.path = "states/openworld/maps/"
map = AdvTiledLoader.load("beginning.tmx")
map:setDrawRange(0, 0, map.width * map.tileWidth, map.height * map.tileHeight)
camera:setBounds(0, 0, map.width * map.tileWidth - love.graphics.getWidth(), map.height * map.tileHeight - love.graphics.getHeight() )
playerL = love.graphics.newImage("textures/playerL.png")
playerD = love.graphics.newImage("textures/playerD.png")
playerR = love.graphics.newImage("textures/playerR.png")
playerU = love.graphics.newImage("textures/playerU.png")
world = {
ground = 512,
}
player = {
x = 94,
y = 3100,
x_vel = 0,
y_vel = 0,
speed = 256,
state = "",
h = 50,
w = 50,
standing = false,
image = love.graphics.newImage("textures/playerR.png")
}
self = player
function player:update(dt)
local halfX = self.w / 2
local halfY = self.h / 2
self.x_vel = math.clamp(self.x_vel, -self.speed, self.speed)
self.y_vel = math.clamp(self.y_vel, -self.speed, self.speed)
local nextY = self.y + (self.y_vel*dt)
if self.y_vel < 0 then
if not (self:isColliding(map, self.x - halfX, nextY - halfY))
and not (self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
self.y = nextY
self.standing = false
else
self.y = nextY + map.tileHeight - ((nextY - halfY) % map.tileHeight)
self:collide("ceiling")
end
end
if self.y_vel > 0 then
if not (self:isColliding(map, self.x-halfX, nextY + halfY))
and not(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
self.y = nextY
self.standing = false
else
self.y = nextY - ((nextY + halfY) % map.tileHeight)
self:collide("floor")
end
end
local nextX = self.x + (self.x_vel * dt)
if self.x_vel > 0 then
if not(self:isColliding(map, nextX + halfX, self.y - halfY))
and not(self:isColliding(map, nextX + halfX, self.y + halfY - 1)) then
self.x = nextX
else
self.x = nextX - ((nextX + halfX) % map.tileWidth)
end
elseif self.x_vel < 0 then
if not(self:isColliding(map, nextX - halfX, self.y - halfY))
and not(self:isColliding(map, nextX - halfX, self.y + halfY - 1)) then
self.x = nextX
else
self.x = nextX + map.tileWidth - ((nextX - halfX) % map.tileWidth)
end
end
self.state = self:getState()
end
function player:collide(event)
if event == "floor" then
self.y_vel = 0
self.standing = true
end
if event == "ceiling" then
self.y_vel = 0
end
end
function player:isColliding(map, x, y)
local layer = map.tl ["Solid"]
local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
local tile = layer.tileData (tileX, tileY)
return not(tile == nil)
end
function player:getState()
local tempState = ""
if self.standing then
if self.x_vel > 0 then
tempState = "right"
elseif self.x_vel < 0 then
tempState = "left"
else
tempState = "stand"
end
end
if self.y_vel > 0 then
tempState = "fall"
elseif self.y_vel < 0 then
tempState = "jump"
end
return tempState
end
end
function love.draw()
camera:set()
love.graphics.draw(player.image, player.x ,player.y)
love.graphics.setColor( 255, 255, 255 )
map:draw()
camera:unset()
end
function love.update(dt)
if dt > 0.05 then
dt = 0.05
end
if love.keyboard.isDown("d") then
self.x_vel = self.speed
player.image = playerR
elseif love.keyboard.isDown("a") then
self.x_vel = -1 * self.speed
player.image = playerL
end
if love.keyboard.isDown("w") then
self.y_vel = -1 * self.speed
player.image = playerU
elseif love.keyboard.isDown("s") then
self.y_vel = 1 * self.speed
player.image = playerD
end
player:update(dt)
camera:setPosition( player.x - (love.graphics.getWidth()/2), player.y - (love.graphics.getHeight()/2))
end
function love.keyreleased(key)
if (key == "a") or (key == "d") then
player.x_vel = 0
end
if (key == "w") or (key == "s") then
player.y_vel = 0
end
end
Code: Select all
elseif love.keyboard.isDown("s") then
self.y_vel = 1 * self.speed
player.image = playerD
end
player:update(dt)
camera:setPosition( player.x - (love.graphics.getWidth()/2), player.y - (love.graphics.getHeight()/2))
Code: Select all
function clearLoveCallBacks()
love.draw = nil
love.joystickpressed = nil
love.joystickreleased = nil
love.keypressed = nil
love.keyreleased = nil
--love.load = nil
love.mousepressed = nil
love.mousereleased = nil
love.update = nil
end
state = {}
function loadState(name)
state = {}
clearLoveCallBacks()
local path = "states/" .. name
require (path .. "/main")
load()
end
function load()
loadState("menu")
end
function love.load()
loadState("menu")
end
function love.draw()
end
function love.update(dt)
end
function love.focus(bool)
end
function love.keypressed(key, unicode)
end
function love.keyreleased(key)
end
function love.mousepressed(x, y)
end
function love.mousereleased(x, y)
end
function love.quit()
end
function love.draw()
end
Dave