为什么 TIC-80 让我尝试索引 nil 值错误?

Why is TIC-80 giving me the attempt to index a nil value error?

所以我正在为 itch.io 的 game jam 制作一个游戏,我正在使用这个非常简洁的 Fantasy Console,叫做 TIC-80,可以在 tic80.com 找到。我的问题是,虽然我了解错误消息是什么及其含义,但我不明白为什么会出现此错误。

错误消息(在 TIC-80 控制台中):

>[string "-- title:  The Clone 
Wars..."]:144: attempt to index a nil 
value (field '?')
stack traceback:
    [string "-- title:  The Clone 
Wars..."]:144: in function 'TIC'

相关代码:

-- title:  The Clone Wars
-- author: DinoNuggies
-- desc:   Help the jedi destroy the clones!
-- script: lua

--Functions
function sign(n) return n>0 and 1 or n<0 and -1 or 0 end
function lerp(a,b,t) return (1-t)*a + t*b end
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
--End of Functions


--Variables
player = {
    spr = 256,
    sprMod = 0,
    x = 1,
    y = 1,
    vx = 0,
    vy = 0,
    dirX = 0,
    dirY = 0,
    flip = 0,
    shoot = false,
}
gun = {
    t = 0,
    spr = 258,
    sprMod = 0,
    x = 0,
    y = 0,
    modX = 0,
    modY = 0,
    flip = 0,
    rot = 0,
}
tile = {
    r0 = 0,
    r1 = 0,
    l0 = 0,
    l1 = 0,
    u0 = 0,
    u1 = 0,
    d0 = 0,
    d1 = 0,
    m = 0,
}
m = {
    x = 0,
    y = 0,
    left = false,
    right = false,
    middle = false,
}
cam = {
    activate = true,
    x = 120,
    y = 64,
}

    --Bullet Class & Functions
bulletMod = 0
bullet = {}
function bullet:new()
    local this = {
        spr = 260,
        x = player.x,
        y = player.y,
        vx = 0,
        vy = 0,
        mx = m.x - (player.x + cam.x),
        my = m.y - (player.y + cam.y),
        t = 0,
    }
    return this
end

function bullet:remove()
    local this = {
        spr = nil,
        x = nil,
        y = nil,
        vx = nil,
        vy = nil,
        mx = nil,
        my = nil,
        t = nil,
    }
    return this
end
--End of Variables

function TIC()
    cls()

    --Camera
    if cam.activate then
        cam.x = math.min(120, lerp(cam.x, 120-player.x, 0.05))
        cam.y = math.min(64, lerp(cam.y, 64-player.y, 0.05))
        ccx = cam.x / 8 + (cam.x % 8 == 0 and 1 or 0)
        ccy = cam.y / 8 + (cam.y % 8 == 0 and 1 or 0)
    end
    map(15 - ccx, 8 - ccy, 31, 18, (cam.x % 8) - 8, (cam.y % 8) - 8, 0)
    --End of Camera


    --Gun Physics
    m.x, m.y, m.left, m.middle, m.right = mouse()
    gun.x = player.x + cam.x
    gun.y = player.y + cam.y
    bullets = tablelength(bullet) - 2
    flick = (time() // 16) % 16 == 0
    if m.left then m.leftp = flick else m.leftp = false end

        --Gun Display
    if m.x > player.x + cam.x + 4 then
        gun.flip = 0
        gun.modX = 4
    else
        gun.flip = 1
        gun.modX = -4
    end

        --Gun Firing
    if m.leftp == true then
        bullet[bullets] = bullet:new()
        if m.x > player.x + cam.x + 4 then
            bullet[bullets].vx = 8
        else
            bullet[bullets].vx = -8
        end
    end
    --End of Gun Physics


    --Bullet Physics
    if bullets > 0 then
        for i=0,bullets-1 do

            bullet[i].x = bullet[i].x + bullet[i].vx --LINE 144, THE ONE IN QUESTION
            bullet[i].y = bullet[i].y + bullet[i].vy

            spr(bullet[i].spr, bullet[i].x + cam.x, bullet[i].y + cam.y, 0, 1)

            bullet[i].t = bullet[i].t + 1
            if bullet[i].t > 16 then
                bullet[i] = bullet:remove()
                bullet[i] = nil
            end
        end
    end
    bullets = tablelength(bullet) - 2
    --End of Bullet Physics


    --Drawing
        --Sprites
    spr(player.spr + player.sprMod, player.x + cam.x, player.y + cam.y, 0, 1, player.flip)
    spr(gun.spr + gun.sprMod, gun.x + gun.modX, gun.y + gun.modY, 0, 1, gun.flip, gun.rot)

        --Debug
    print("Debug:", 1, 1, 11)
    print("X: " .. (player.x // 8) + 15 .. " Y: " .. (player.y // 8) + 9, 1, 8, 11)
    print("BLTs: " .. bullets .. " ", 1, 16, 11)
    --End of Drawing


    --Player Movement
    player.x = player.x + player.vx
    player.y = player.y + player.vy
    if key(1) and player.x > -120 then
        player.vx = -1
        player.sprMod = 1
        player.dirX = 0
    elseif key(4) then 
        player.vx = 1
        player.sprMod = 0
        player.dirX = 1
    else
        player.vx = 0
    end
    if key(23) and player.y > -64 then
        player.vy = -1
        player.dirY = 0
    elseif key(19) then
        player.vy = 1
        player.dirY = 1
    else
        player.vy = 0
    end

    if btnp(0) then player.y = player.y - 1
    elseif btnp(1) then player.y = player.y + 1
    elseif btnp(2) then player.x = player.x - 1
    elseif btnp(3) then player.x = player.x + 1
    end
    --End of Movement


    --Player Collision
    tile.r0 = mget(((player.x + 7) // 8) + 15, ((player.y - 9) // 8) + 9)
    tile.r1 = mget(((player.x + 7) // 8) + 15, ((player.y - 2) // 8) + 9)
    tile.l0 = mget(((player.x - 2) // 8) + 15, ((player.y - 9) // 8) + 9)
    tile.l1 = mget(((player.x - 2) // 8) + 15, ((player.y - 2) // 8) + 9)
    tile.u0 = mget(((player.x - 1) // 8) + 15, ((player.y - 10) // 8) + 9)
    tile.u1 = mget(((player.x + 6) // 8) + 15, ((player.y - 10) // 8) + 9)
    tile.d0 = mget(((player.x - 1) // 8) + 15, ((player.y - 1) // 8) + 9)
    tile.d1 = mget(((player.x + 6) // 8) + 15, ((player.y - 1) // 8) + 9)

    if player.dirX == 1 then
        if fget(tile.r0, 0) or fget(tile.r1, 0) then
            player.vx = 0
        end
    elseif player.dirX == 0 then
        if fget(tile.l0, 0) or fget(tile.l1, 0) then
            player.vx = 0
        end
    end

    if player.dirY == 0 then
        if fget(tile.u0, 0) or fget(tile.u1, 0) then
            player.vy = 0
        end
    elseif player.dirY == 1 then
        if fget(tile.d0, 0) or fget(tile.d1, 0) then
            player.vy = 0
        end
    end
    --End of Player Collision


    --Misc
    if keyp(3) then
        if cam.activate then
            cam.activate = false
        else
            cam.activate = true
        end
    end
    --End of Misc
end

--DON'T WORRY ABOUT ANYTHING PAST THIS POINT, IT'S ONLY SPRITE AND TILE DEFINITIONS FOR TIC-80 VISUALS

-- <TILES>
-- 000:6666666666666666666666666666666666666666666666666666666666666666
-- 001:6666666666566566666666656666666665665666666665666566666666666666
-- 002:6668666666898566666866656666666665665866686689868986686668666666
-- 003:6656556666555556555556555555556555555555665555556556555666655566
-- 016:dddddddeddddddefddeeeeffddeeeeffddeeeeffddeeeeffdeffffffefffffff
-- 017:6226622661266126222222221121112161266126612661265126512665266525
-- 018:6228622661298126222222221121112161266126612681268126512668266525
-- 019:6226522661255126222222221121112151255125612551255126512665255525
-- </TILES>

-- <SPRITES>
-- 000:00cccc000cccccd00cbbbbb0ccbbcbbdcccfefcd0dddddd000ceec0000c00c00
-- 001:00cccc000cccccd00bbbbbd0cbbcbbcdccfefccd0dddddd000ceec0000c00c00
-- 002:000000000000000000000000000eeeed000efff0000ef0000000000000000000
-- 003:0000ed00000eef0000eef0000eef000000eef000000ee0000000000000000000
-- 004:0000000000000000000000000aaaaaa00aaaaaa0000000000000000000000000
-- </SPRITES>

-- <MAP>
-- 000:010101010101010101010101010101010101010101010101010101010101010000000000000000000000101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 001:010000000000000000000000000000101000101000001000000010101001010000000000100000000000101010000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 002:010001000100010000000000000010000000100010001000100010001001010000000010100000000000101010000000000000001010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 003:010001000100000000000000000000101000100010001000100010100001010000000010000000000000101000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 004:010001010100010000000000000000001000100010001000100010000001010000000010000000000000101010000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 005:010001000100010000000000000010100000001010000010100010000001010000000010000000000000101010000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 006:010000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 007:010000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 008:010000000000000000000000000101301010000000000000000000000000000000000000101010000000101000000000000000000010101000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 009:010000000000000000000000000101101010100000000000000000000000000000001010101000000000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 010:010000000000000000000000000101101010000000000001111101000000001111111121211111110000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 011:010000000000000000000000000101000000000000001000000001000000000000001020101000000000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 012:010000000000000000000000000101000000000000011111111101010001010000101010000000000010101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 013:010000000000000000000000000101000000000010101000000001011101010000101000000000000010100000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 014:010000000000000000000000000101000000000010101010000000000000010000000000000000000010100000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 015:010000000000000000000000000101000000000000000000000000000000010000000000000000001010000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 016:010101010101010101010101010101000000000000000000000000000000010000000000000000001010000000000000001010000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 017:010101010101010101010101010101000000000000000000000000000000000000000000000000101010000000000010102000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 018:000000000000000000000000000101000000000000000000000010100000000000000000000000101000000000001010000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 019:000000000000000000001010000101000000000000000000000000100000000000000000000010101000001111211110000000001010000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 020:000000001000000000001000000101000000000000000000000000101000000000000000001010101000000010101011111100000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 021:000000000000000000001010100101101010000000000000000000000000000000000000101000000000001010200000000011110000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 022:000000000000000000000000000101001010100000000000000000000000000000000010101000000010101010100000000000001111000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 023:000000000000000000000000000101000000000000000000000000000000000000001010100000000010101010000000000000000000110000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 024:000000000000000000000000000101000000000000000000000000000000000000001010100000001020201000000000000000100000001100102001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 025:000000000000000000000000000101000000000000000000000000000000000000101010000000001020100000000000000000100000000011212101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 026:000000101000000000000000000101000000000000000000000000000000001010100010000000001010100000000010000000100010100010101001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 027:000000101010000000000000100101000000000000000010101010101010101010000010000000001010000000000000101010000000001010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 028:000000100010000000000000100101000000000000000000000000001010101000101010000000001010000010101010000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 029:000000001010000000000000000101000000000000000000000000000010100000000000000000001010000000000000000000100010000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 030:000000001010000000000000000101100000000000000000000000000000000000000000000000001000000000001000000000100000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 031:000000001010000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 032:000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 033:000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 135:010101010101010101010101010101010101010101010101010101010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- </MAP>

-- <WAVES>
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>

-- <SFX>
-- 000:020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200304000000000
-- 001:020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200307000000000
-- 002:02000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020030b000000000
-- </SFX>

-- <FLAGS>
-- 000:00000000000000000000000000000000101010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- </FLAGS>

-- <PALETTE>
-- 000:1a1c2c5d2810814428ca9581ffcd7575ae442495482571793c55c22c9dfae20000000000f4f4f494b0c2566c86333c57
-- </PALETTE>


触发错误的是玩家发射第二颗子弹之后。发射一颗后,子弹在队列中创建、显示和消失,但在第二颗子弹开始存在后,它停止并给我错误。我已经改变了很多东西,似乎每次我引用一个项目符号对象值时,当存在多个项目符号对象值时,它会向我发送错误,这是我不明白的,因为我想我已经用 for 循环解决了这个问题。

因此,如果您马上发现任何看起来不太正确的地方,请告诉我,如果您对 TIC-80 或 API 的作用一无所知,我我确信 TIC-80 网站可以比我更好地解释它。

如果您想 运行 游戏,查看实际问题并修改代码,请从网站下载 TIC-80 和 运行 此文件: https://drive.google.com/file/d/18ti0NboNNN9Yog6l_n73usF_eX_86EN4/view?usp=sharing

来看看你的子弹处理能力

第一次调用 TIC:

bullets为0,我们在bullet[0]

中加一颗子弹

第二次通话:

bullets为1,我们在bullet[1]中加一颗子弹。 现在,作为 bullets > 0 我们进入子弹物理 if 语句。

我们对该项目符号进行一些计算并递增 bullet[0].t

下面的调用我们也是一样的。我们添加一个项目符号并像上面那样处理除新项目符号之外的所有项目符号。 当项目符号的 t 字段变为 > 16 时,我们将其从 bullet.

中删除

所以我们首先删除最旧的项目符号 bullet[0]。但在接下来的调用中,我们再次从 i = 0 开始循环。所以 bullet[i]nil 因此在 bullet[i].x 中索引它会导致观察到的错误。

旁注:

这没有意义。

function bullet:remove()
    local this = {
        spr = nil,
        x = nil,
        y = nil,
        vx = nil,
        vy = nil,
        mx = nil,
        my = nil,
        t = nil,
    }
    return this
end

一个全为 nil 值的 table 只是一个空的 table。所以简单地 return 一个空的 table.

最重要的是,您不需要该功能,因为它没有任何用处。

bullet[i] = bullet:remove()
bullet[i] = nil

第一行不是必需的。您不必先分配一个 emtpy table 然后再分配 nil。只需分配 nil.

如果您只是将项目符号保留在 table 的数组部分中,则不需要您自己的 tablelength 函数然后减去 2 btw。 然后,您还可以使用 table.remove 删除项目符号,而不会在项目符号列表中产生意外的空白。