错误:尝试索引全局'playerBul'(零值)

时间:2015-07-08 12:43:22

标签: lua

错误在于:

  

game.lua:171:尝试索引全局'playerBul'(零值)

很抱歉,如果我放了太多不必要的代码,我只是不知道是什么导致了这个错误。

以下是此游戏的所有文件:

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)
for i = 1,#options do 
    if i == selection then 
        love.graphics.draw(pointer, 300, 200 + i *20)
    end 
    love.graphics.printf(options[i].text,0,200 + i * 20,   love.graphics.getWidth(), 'center')
end 
end 

function menu.keypressed(key)
if key == "up" then 
    selection = (selection - 2)%(#options) + 1 
elseif key == "down" then 
    selection = (selection) % (#options) + 1 
elseif key == "return" then 
    options[selection].action() 
elseif key == "escape" then
     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)
for i = #t,1,-1 do 
    table.remove(t,i)
end
end

local function clearAliens()
for i=#aliens,1,-1 do 
    for j = #aliens[i],1,-1 do 
        table.remove(aliens[i],j)
    end
end
end

local function insertAliens()
alienQty = 0 
for i=1,4 do 
    for j =1,10 do 
        local alien = {}
        alien.x = (j-1) *alienUFO.width + 10 
        alien.y = (j-1) *alienUFO.width + 35 
        if level ==2 then 
            alien.life = 2 
        if level == 3 then 
            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 then 
    for i = #playerBullets,1,-1 do 
        playerBullets[i].y = playerBullets[i].y - dt*           playerBullets.bulletSpeed   
        if playerBullets[i].y < 0 then 
            table.remove(playerBullets,i)
        else 
            for j = #aliens, 1,-1 do 
                for k = #aliens[j],1,-1 do 
                    if next(playerBullets) ~= nil and playerBullets[i] ~= nil     and
                        aliens[j][k].x <= playerBullets[i].x and aliens[j]  [k].x + 50 >= playerBullets[i].x
                        and aliens[j][k].y <= playerBullets[i].y and aliens[j]  [k].y + 47 >= playerBullets[i].y then 
                        table.remove(playerBullets, i)
                        if level == 2 and aliens[j][k].life > 0 then 
                            aliens[j][k].life = aliens[j][k].life - 1 
                        if level == 3 and aliens[j][k].life > 0 then 
                            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 then 
    love.event.quit() -- COME BACK HERE FOR LOSE 
end
end

local function AlienShots(dt)
if next(alienBullets) ~= nil then 
    for i = #alienBullets,1,-1 do 
        alienBullets[i].y = alienBullets[i].y + dt * alienUFO.bulletSpeed
        if alienBullets[i].y > love.graphics.getHeight() then 
            table.remove(alienBullets, i)
        elseif playerShip.x <= alienBullets[i].x and playerShip.x +   playerShip.width >= alienBullets[i].x 
            and playerShip.y <= alienBullets[i].y and playerShip.y +  playerShip.height >- alienBullets[i].y then 
            table.remove(alienBullets, i)
            PlayerShot()
        end
    end
end
end

local function nextLVL()
level = level + 1 
if level == 2 then 
    alienUFO.image = love.graphics.newImage(alienUFO.ENEMY1)
    alienUFO.speed = 100 
    alienUFO.shotProb = 20
    alienUFO.bullet = alienBul.image 
    initGame()
elseif level == 3 then 
    alienUFO.image = love.graphics.newImage(alienUFO.ENEMY2)
    initGame()
elseif level > 3 then 
    love.event.quit() -- COME BACK HERE FOR 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 then 
    love.graphics.print('GAME PAUSED',love.graphics.getWidth()/2 - 20,20)
end 

if next(playerBullets) ~= nil then 
    for i = 1, #playerBullets do 
        love.graphics.draw(playerBul.image, playerBullets[i].x,    playerBullets[i].y)
    end
end

if next(alienBullets) ~= nil then 
    for i = 1, #alienBullets do 
        love.graphics.draw(alienBul.image, alienBullets[i].x,    alienBullets[i].y) 
    end
end

if next(aliens) ~= nil then 
    for i = 1, #aliens do 
        for j = 1, #aliens[i] do 
            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)
for i = 1, lives do 
    love.graphics.draw(healthImage, 10+ 1 * 15,20)
end 
end 

function game.update(dt)
    if not pause then 
        if alienQty == 0 then 
            nextLVL()
        end 

        if love.keyboard.isDown("right") then 
            playerShip.x = playerShip.x + dt * playerShip.speed 
        elseif love.keyboard.isDown("left") then 
            playerShip.x = playerShip.x - dt * playerShip.speed 
        end 
        if love.keyboard.isDown(" ") then 
            playerShoot() 
        end 
        playerShots(dt)
        AlienShots(dt)
        if love.math.random(1,100) < alienUFO.shotProb then 
            local r = love.math.random(1,#aliens)
            if next(aliens[r]) ~= nil then 
                alienShoot(aliens[r][#aliens[r]].x, aliens[r][#aliens[r]].y)
            end 
        end 

        if alienMovement then 
            for i = 1, #aliens do 
                for j = 1, #aliens[i] do 
                    aliens[i][j].x = aliens[i][j].x + dt * alienUFO.speed
                    if aliens[i][j].x + alienUFO.width + 2 >=    love.graphics.getWidth() then 
                        alienMovement = false 
                    end
                end
            end
        else 
            for i =1, #aliens do 
                for j = 1, #aliens[i] do 
                    aliens[i][j].x = aliens[i][j].x - dt * alienUFO.speed 
                    if aliens[i][j].x - 2 <= 0 then 
                        alienMovement = true 
                    end
                end
            end
        end
        if playerShip.x < 0 then 
            playerShip.x = 0 
            elseif playerShip.x > love.graphics.getWidth() - 75 then 
                playerShip.x = love.graphics.getWidth() -75
            end 
        end 
    end 
end

function game.keypressed(key)
    if key =='p' then 
        pause = not pause 
    end 
end

return game 

1 个答案:

答案 0 :(得分:3)

您尚未在load返回的游戏桌上定义game.lua功能。看起来您已经编写了加载逻辑,但将其编写为名为loadGame的本地函数。修复你的问题更新game.lua以导出加载函数,并让它调用你已经创建的加载函数,如下所示:

function game.load()
    loadGame()
end

在您返回game.lua中的游戏桌之前的某个地方,请在您定义game.keypressed之后说明

或者您也可以将local function loadGame()更改为function game.load()以删除本地功能并将其重新定义为导出功能

相关问题