love2d - Lua Error "Attempt to call method '' (a nil Value) -
i wanted make 2d-platformer (with love2d-framework) , have main.lua-file select random maps states folder. start that, said main-main.lua should open main.lua-file in /states/map1/. every time try run it, error-message:
error
states/map1/main.lua:169: attempt call method 'update' (a nil value)
traceback
states/map1/main.lua:169: in function 'update' [c]: in function 'xpcall'
the main-main.lua-code:
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() local player end function load() end function love.load() loadstate("map1") end
the main.lua in /states/map1/-file:
player = table local advtiledloader = require("advtiledloader.loader") require("camera") function love.load() love.graphics.setbackgroundcolor( 198, 220, 255 ) song1 = love.audio.newsource("sound/brightly_fancy.mp3", true) song1:setvolume(0.1) song1:play() imgplayer = love.graphics.newimage("textures/player.png") advtiledloader.path = "maps/" map = advtiledloader.load("map1.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() ) world = { gravity = 1536, --1536 ground = 512, } player = { x = 256, y = 256, x_vel = 0, y_vel = 0, jump_vel = -1024, speed = 512, --512 flyspeed = 700, state = "", h = 32, w = 32, standing = false, } function player:jump() if self.standing self.y_vel = self.jump_vel self.standing = false end end function player:right() self.x_vel = self.speed end function player:left() self.x_vel = -1 * (self.speed) end function player:stop() self.x_vel = 0 end function player:collide(event) if event == "floor" self.y_vel = 0 self.standing = true end if event == "cieling" self.y_vel = 0 end end function player:update(dt) local halfx = self.w / 2 local halfy = self.h / 2 self.y_vel = self.y_vel + (world.gravity * dt) self.x_vel = math.clamp(self.x_vel, -self.speed, self.speed) self.y_vel = math.clamp(self.y_vel, -self.flyspeed, self.flyspeed) local nexty = self.y + (self.y_vel*dt) if self.y_vel < 0 if not (self:iscolliding(map, self.x - halfx, nexty - halfy)) , not (self:iscolliding(map, self.x + halfx - 1, nexty - halfy)) self.y = nexty self.standing = false else self.y = nexty + map.tileheight - ((nexty - halfy) % map.tileheight) self:collide("cieling") end end if self.y_vel > 0 if not (self:iscolliding(map, self.x-halfx, nexty + halfy)) , not(self:iscolliding(map, self.x + halfx - 1, nexty + halfy)) 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 if not(self:iscolliding(map, nextx + halfx, self.y - halfy)) , not(self:iscolliding(map, nextx + halfx, self.y + halfy - 1)) self.x = nextx else self.x = nextx - ((nextx + halfx) % map.tilewidth) end elseif self.x_vel < 0 if not(self:iscolliding(map, nextx - halfx, self.y - halfy)) , not(self:iscolliding(map, nextx - halfx, self.y + halfy - 1)) self.x = nextx else self.x = nextx + map.tilewidth - ((nextx - halfx) % map.tilewidth) end end self.state = self:getstate() 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 if self.x_vel > 0 tempstate = "right" elseif self.x_vel < 0 tempstate = "left" else tampstate = "stand" end end if self.y_vel > 0 tempstate = "fall" elseif self.y_vel < 0 tempstate = "jump" end return tempstate end end function love.draw() camera:set() love.graphics.setcolor( 255, 255, 255, 255 ) love.graphics.draw( imgplayer, player.x - player.w/2, player.y - player.h/2, 0, 1, 1, 0, 0) love.graphics.setcolor( 255, 255, 255 ) map:draw() camera:unset() end function love.update(dt) if dt > 0.05 dt = 0.05 end if love.keyboard.isdown("d") player:right() end if love.keyboard.isdown("a") player:left() end if love.keyboard.isdown("w") --and not (hasjumped) player:jump() 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") player.x_vel = 0 end end
i dont know problem :/
your code not going work way think works. call love.load
loads map specific file sets own love.load
, love.update
. overwrites love.load
had before, change has no effect love.load
called once, map-specific initialization have there not executed, map-specific love.update
code is executed, fails it's missing initialization part. need restructure code such have 1 love.load
, love.update
, call map-specific functions needed. suggest @ examples of love2d games see how organized.