为啥 TIC-80 让我尝试索引零值错误?

Posted

技术标签:

【中文标题】为啥 TIC-80 让我尝试索引零值错误?【英文标题】:Why is TIC-80 giving me the attempt to index a nil value error?为什么 TIC-80 让我尝试索引零值错误? 【发布时间】:2022-01-19 23:18:13 【问题描述】:

因此,我正在 itch.io 上为 Game Jam 开发一款游戏,我正在使用这款名为 TIC-80 的非常简洁的 Fantasy 控制台,可在 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

【问题讨论】:

一些 bullet[i] 在第 144 行是 nil。您可以通过在第 #156 行之前插入代码来“压缩”bullet 数组(将 nil 移到末尾)来修复它 【参考方案1】:

让我们看看你的子弹处理能力

第一次致电TIC

bullets 为 0。我们在 bullet[0] 中添加一个项目符号

第二次通话:

bullets 是 1。我们在bullet[1] 中添加了一个项目符号。 现在,作为bullets &gt; 0,我们输入子弹物理 if 语句。

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

下面的调用我们做同样的事情。我们添加一个项目符号并处理除新项目符号之外的所有项目,就像我们上面所做的那样。 当子弹的 t 字段变为 &gt; 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 值的表只是一个空表。所以只需返回一个空表。

除此之外,您不需要该功能,因为它没有任何用处。

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

第一行是不必要的。您不必先分配一个空表,然后再分配 nil。只需分配nil

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

【讨论】:

谢谢,我终于明白了,for 循环总是从 0 开始,而 bullet[0] 并不总是存在,即使 bullet[1] 存在。 TYSM! Game Jam 将在 2 天内到期,祝我好运! 我有一个问题,有没有办法可以将表格向左移动?示例:bullet[0]bullet[1] 存在,很快 bullet[0] 被删除,然后将所有内容从 bullet[1] 移动到新的空 bullet[0] 和理论上 bullet[2]bullet[1] 如果存在,你知道吗我是说? 不要从 0 开始你的表,而是从 1 开始。然后你可以使用 table.remove 来实现这一点

以上是关于为啥 TIC-80 让我尝试索引零值错误?的主要内容,如果未能解决你的问题,请参考以下文章

LUA 错误:尝试索引零值 @6:16

Lua 尝试索引? (零值)

尝试索引全局 'io'(零值)

Lua 脚本 - 尝试索引全局(零值)

Lua - util_server.lua:440 尝试索引本地“自我”(零值)

尝试索引全局'ENT'(零值)在一定时间后删除实体