lua - Error: attempt to index global 'playerBul' (a nil value) -
the error saying:
game.lua:171: attempt index global 'playerbul' (a nil value)
sorry if put unnecessary code, not know causing error.
here files game:
menu.lua:
local menu = {} local bannermenu; local selection; menu.name = 'menu' local function play() mode = require('game') mode.load() end local options = { {['text'] = 'play', ['action'] = play}, {['text'] = 'exit', ['action'] = love.event.quit} } function menu.load() bannermenu = love.graphics.newimage(banner) selection = 1 pointer = love.graphics.newimage(pointer) mode = menu end function menu.update() return mode end function menu.draw() love.graphics.draw(bannermenu, 200,10) = 1,#options if == selection love.graphics.draw(pointer, 300, 200 + *20) end love.graphics.printf(options[i].text,0,200 + * 20, love.graphics.getwidth(), 'center') end end function menu.keypressed(key) if key == "up" selection = (selection - 2)%(#options) + 1 elseif key == "down" selection = (selection) % (#options) + 1 elseif key == "return" options[selection].action() elseif key == "escape" love.event.quit() end end return menu main.lua
title = 'die aliens die!' player_bullet = 'images/playerbullet.png' enemy_bullet = 'images/enemybullet.png' health_img = 'images/life.png' enemy_ufo = { enemy_1 = 'images/enemy1.png', enemy_2 = 'images/enemy2.png', enemy_3 = 'images/enemy3.png' } player_img = 'images/spaceship.png' you_win = 'images/win.png' banner = 'images/banner.png' pointer = 'images/pointer.png' function love.load() love.window.settitle(title) mode = require "menu" mode.load() end function love.draw() mode.draw() end function love.keypressed(key, isrepeat) mode.keypressed(key) end function love.update(dt) mode.update(dt) end game.lua
local game = {} local playership = {} local alienufo = {} local bullets = {} local alienbullets = {} local playerbullets = {} local aliens = {{},{},{},{},{},{},{},{},{},{}} local health = 3 local score = 0 local level = 1 local alienmovement = true local healthimage; local alienqty = 0 local pause --local function gameover() --local function gamwwin() local function cleartable(t) = #t,1,-1 table.remove(t,i) end end local function clearaliens() i=#aliens,1,-1 j = #aliens[i],1,-1 table.remove(aliens[i],j) end end end local function insertaliens() alienqty = 0 i=1,4 j =1,10 local alien = {} alien.x = (j-1) *alienufo.width + 10 alien.y = (j-1) *alienufo.width + 35 if level ==2 alien.life = 2 if level == 3 alien.life = 3 end table.insert(aliens[j], alien) alienqty = alienqty + 1 end end end end local function initgame() cleartable(playerbullets) cleartable(alienbullets) clearaliens() insertaliens() playership.x = love.graphics.getwidth() / 2 - playership.width /2 end local function playershoot() local bullet = {} playership.x = playership.x + 32 playership.y = playership.y + 10 table.insert(playerbullets, bullet) end local function alienshoot(x,y) local bullet = {} bullet.x = alienufo.width/2 bullet.y = y -5 table.insert(alienbullets, bullet) end local function playershots(dt) if next(playerbullets) ~= nil = #playerbullets,1,-1 playerbullets[i].y = playerbullets[i].y - dt* playerbullets.bulletspeed if playerbullets[i].y < 0 table.remove(playerbullets,i) else j = #aliens, 1,-1 k = #aliens[j],1,-1 if next(playerbullets) ~= nil , playerbullets[i] ~= nil , aliens[j][k].x <= playerbullets[i].x , aliens[j] [k].x + 50 >= playerbullets[i].x , aliens[j][k].y <= playerbullets[i].y , aliens[j] [k].y + 47 >= playerbullets[i].y table.remove(playerbullets, i) if level == 2 , aliens[j][k].life > 0 aliens[j][k].life = aliens[j][k].life - 1 if level == 3 , aliens[j][k].life > 0 aliens[j][k].life = aliens[j][k].life - 1 else table.remove(aliens[j], k) alienqty = alienqty - 1 score = score + 100 end break end end end end end end end local function playershot() health = health - 1 if health == 0 love.event.quit() -- come here lose end end local function alienshots(dt) if next(alienbullets) ~= nil = #alienbullets,1,-1 alienbullets[i].y = alienbullets[i].y + dt * alienufo.bulletspeed if alienbullets[i].y > love.graphics.getheight() table.remove(alienbullets, i) elseif playership.x <= alienbullets[i].x , playership.x + playership.width >= alienbullets[i].x , playership.y <= alienbullets[i].y , playership.y + playership.height >- alienbullets[i].y table.remove(alienbullets, i) playershot() end end end end local function nextlvl() level = level + 1 if level == 2 alienufo.image = love.graphics.newimage(alienufo.enemy1) alienufo.speed = 100 alienufo.shotprob = 20 alienufo.bullet = alienbul.image initgame() elseif level == 3 alienufo.image = love.graphics.newimage(alienufo.enemy2) initgame() elseif level > 3 love.event.quit() -- come here win end end function game.load() pause = false level = 1 health = 3 score = 0 playership.speed = 300 playership.bulletspeed = 300 playership.width = 75 playership.height = 71 alienufo.speed = 70 alienufo.width = 50 alienufo.height = 47 alienufo.enemy1 = enemy_ufo.enemy_1 alienufo.enemy2 = enemy_ufo.enemy_2 alienufo.enemy3 = enemy_ufo.enemy_3 alienufo.bulletspeed = 300 alienufo.image = love.graphics.newimage(alienufo.enemy3) playership.image = player_img playership.image = love.graphics.newimage(playership.image) healthimage = love.graphics.newimage(health_img) playership.x = love.graphics.getwidth() / 2 - playership.width/2 playership.y = love.graphics.getheight() - 100 playerbul.image = love.graphics.newimage(player_bullet) alienbul.image = love.graphics.newimage(enemy_bullet) initgame() end function game.draw() if pause love.graphics.print('game paused',love.graphics.getwidth()/2 - 20,20) end if next(playerbullets) ~= nil = 1, #playerbullets love.graphics.draw(playerbul.image, playerbullets[i].x, playerbullets[i].y) end end if next(alienbullets) ~= nil = 1, #alienbullets love.graphics.draw(alienbul.image, alienbullets[i].x, alienbullets[i].y) end end if next(aliens) ~= nil = 1, #aliens j = 1, #aliens[i] love.graphics.draw(alienufo.image, aliens[i][j].x, aliens[i][j].y) end end end love.graphics.print('score: '..score, love.graphics.getwidth() - 100,20) love.graphics.draw(playership.image, playership.x, playership.y) = 1, lives love.graphics.draw(healthimage, 10+ 1 * 15,20) end end function game.update(dt) if not pause if alienqty == 0 nextlvl() end if love.keyboard.isdown("right") playership.x = playership.x + dt * playership.speed elseif love.keyboard.isdown("left") playership.x = playership.x - dt * playership.speed end if love.keyboard.isdown(" ") playershoot() end playershots(dt) alienshots(dt) if love.math.random(1,100) < alienufo.shotprob local r = love.math.random(1,#aliens) if next(aliens[r]) ~= nil alienshoot(aliens[r][#aliens[r]].x, aliens[r][#aliens[r]].y) end end if alienmovement = 1, #aliens j = 1, #aliens[i] aliens[i][j].x = aliens[i][j].x + dt * alienufo.speed if aliens[i][j].x + alienufo.width + 2 >= love.graphics.getwidth() alienmovement = false end end end else =1, #aliens j = 1, #aliens[i] aliens[i][j].x = aliens[i][j].x - dt * alienufo.speed if aliens[i][j].x - 2 <= 0 alienmovement = true end end end end if playership.x < 0 playership.x = 0 elseif playership.x > love.graphics.getwidth() - 75 playership.x = love.graphics.getwidth() -75 end end end end function game.keypressed(key) if key =='p' pause = not pause end end return game
you haven't defined load function on game table return in game.lua. looks wrote load logic wrote local function named loadgame. fix issue update game.lua export load function , have call load function you've created, this:
function game.load() loadgame() end somewhere before return game table in game.lua, after definition of game.keypressed
or alternatively change local function loadgame() function game.load() remove local function , redefine exported functionality
Comments
Post a Comment