错误 main.lua:98:尝试索引本地 'e'(数字值)
Error main.lua:98: attempt to index local 'e' (a number value)
我一直收到这个错误,说我尝试了 tonindex local 'e'(一个数字值),但我不知道我做错了什么。我正在尝试开发一款游戏,你必须射击向地面靠近的外星人,而不是让他们接触地面。我对 Lua 和 Love2d 很陌生,所以如果这是一个简单的修复,我很抱歉。
代码:
function love.load()
ship = love.graphics.newImage("ship.png")
heart = love.graphics.newImage("heart.png")
font = love.graphics.newFont("font.ttf", 20)
player = {}
storage = {}
storage.enemys = {}
level = 1
storage.enemys.cooldown = 400 / level
player.x = 0
player.y = 600
player.bullets = {}
player.cooldown = 20
player.speed = 10
ammo = 3
life = 2
player.fire = function()
if player.cooldown <= 0 then
if ammo > 0 then
ammo = ammo - 1
player.cooldown = 20
bullet = {}
bullet.x = player.x + 35
bullet.y = player.y
table.insert(player.bullets, bullet)
end
end
end
storage.spawn = function()
if enemys.cooldown <= 0 then
storage.enemy.cooldown = 400 / level
enemy = {}
enemy.x = love.math.random(0, 780)
enemy.y = 0
table.insert(storage.enemys, enemy)
end
end
reload = function()
ammo = 3
end
end
function love.update(dt)
player.cooldown = player.cooldown - 1
storage.enemys.cooldown = storage.enemys.cooldown - 1
if player.x >= 720 then
player.x = 720
elseif player.x <= 0 then
player.x = 0
end
if love.keyboard.isDown("f") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("a") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("r") then
reload()
end
if love.keyboard.isDown("space") then
player.fire()
end
for i,b in ipairs(player.bullets) do
if b.y < -10 then
table.remove(player.bullets, i)
end
b.y = b.y - 10
end
for i,e in ipairs(storage.enemys) do
if e.y < 700 then
table.remove(storage.enemys, i)
life = life - 1
end
e.y = e.y - 6
end
if storage.enemys.cooldown <= 0 then
enemys.spawn()
end
end
function love.draw()
love.graphics.draw(ship, player.x, player.y)
for _,e in pairs(storage.enemys) do
love.graphics.setColor(0,255,0)
love.graphics.rectangle("fill", e.x, e.y, 20, 20)
love.graphics.setColor(255, 255, 255)
end
for _,b in pairs(player.bullets) do
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill", b.x, b.y, 2, 6)
love.graphics.setColor(255, 255, 255)
end
if life == 2 then
love.graphics.draw(heart, 0,0)
love.graphics.draw(heart, 25,0)
elseif life == 1 then
love.graphics.draw(heart, 0,0)
end
love.graphics.setFont(font)
love.graphics.print("Ammo", 715, 0)
if ammo == 3 then
love.graphics.rectangle("fill", 670, 20, 125, 25)
elseif ammo == 2 then
love.graphics.rectangle("fill", 711, 20, 84, 25)
love.graphics.rectangle("line", 670, 20, 125, 25)
elseif ammo == 1 then
love.graphics.rectangle("fill", 752, 20, 43, 25)
love.graphics.rectangle("line", 670, 20, 125, 25)
elseif ammo == 0 then
love.graphics.rectangle("line", 670, 20, 125, 25)
end
end
首先
敌人的复数是敌人,不是敌人 ;)
storage.enemies
这个 table 包含一个敌人列表(作为一个序列)以及一个冷却时间值,这是一个数字。
pairs
此函数遍历 table.
的 所有 键值对
问题
你现在可能已经猜到了。 pairs(storage.enemies)
不仅遍历所有敌人(整数键),而且遍历冷却时间(字符串键 'cooldown'
),导致您尝试索引一个数字。
示例:
for key, value in pairs({'a', 'b', 'c', foo='bar'}) do
print(key, value)
end
会打印类似
的内容
1 a
foo bar
3 c
2 b
请注意,顺序是完全随机的,取决于 Lua 如何决定在内部存储 table。
for key, value in ipairs({'a', 'b', 'c', foo='bar'}) do
print(key, value)
end
然而,应该打印:
1 a
2 b
3 c
因为 ipairs
,与 pairs
相反,它只会按顺序遍历整数键,直到达到第一个 nil
值。例如,对于 {1, 2, 3, nil, 5}
它只会迭代 1
、2
和 3
.
我一直收到这个错误,说我尝试了 tonindex local 'e'(一个数字值),但我不知道我做错了什么。我正在尝试开发一款游戏,你必须射击向地面靠近的外星人,而不是让他们接触地面。我对 Lua 和 Love2d 很陌生,所以如果这是一个简单的修复,我很抱歉。
代码:
function love.load()
ship = love.graphics.newImage("ship.png")
heart = love.graphics.newImage("heart.png")
font = love.graphics.newFont("font.ttf", 20)
player = {}
storage = {}
storage.enemys = {}
level = 1
storage.enemys.cooldown = 400 / level
player.x = 0
player.y = 600
player.bullets = {}
player.cooldown = 20
player.speed = 10
ammo = 3
life = 2
player.fire = function()
if player.cooldown <= 0 then
if ammo > 0 then
ammo = ammo - 1
player.cooldown = 20
bullet = {}
bullet.x = player.x + 35
bullet.y = player.y
table.insert(player.bullets, bullet)
end
end
end
storage.spawn = function()
if enemys.cooldown <= 0 then
storage.enemy.cooldown = 400 / level
enemy = {}
enemy.x = love.math.random(0, 780)
enemy.y = 0
table.insert(storage.enemys, enemy)
end
end
reload = function()
ammo = 3
end
end
function love.update(dt)
player.cooldown = player.cooldown - 1
storage.enemys.cooldown = storage.enemys.cooldown - 1
if player.x >= 720 then
player.x = 720
elseif player.x <= 0 then
player.x = 0
end
if love.keyboard.isDown("f") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("a") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("r") then
reload()
end
if love.keyboard.isDown("space") then
player.fire()
end
for i,b in ipairs(player.bullets) do
if b.y < -10 then
table.remove(player.bullets, i)
end
b.y = b.y - 10
end
for i,e in ipairs(storage.enemys) do
if e.y < 700 then
table.remove(storage.enemys, i)
life = life - 1
end
e.y = e.y - 6
end
if storage.enemys.cooldown <= 0 then
enemys.spawn()
end
end
function love.draw()
love.graphics.draw(ship, player.x, player.y)
for _,e in pairs(storage.enemys) do
love.graphics.setColor(0,255,0)
love.graphics.rectangle("fill", e.x, e.y, 20, 20)
love.graphics.setColor(255, 255, 255)
end
for _,b in pairs(player.bullets) do
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill", b.x, b.y, 2, 6)
love.graphics.setColor(255, 255, 255)
end
if life == 2 then
love.graphics.draw(heart, 0,0)
love.graphics.draw(heart, 25,0)
elseif life == 1 then
love.graphics.draw(heart, 0,0)
end
love.graphics.setFont(font)
love.graphics.print("Ammo", 715, 0)
if ammo == 3 then
love.graphics.rectangle("fill", 670, 20, 125, 25)
elseif ammo == 2 then
love.graphics.rectangle("fill", 711, 20, 84, 25)
love.graphics.rectangle("line", 670, 20, 125, 25)
elseif ammo == 1 then
love.graphics.rectangle("fill", 752, 20, 43, 25)
love.graphics.rectangle("line", 670, 20, 125, 25)
elseif ammo == 0 then
love.graphics.rectangle("line", 670, 20, 125, 25)
end
end
首先
敌人的复数是敌人,不是敌人 ;)
storage.enemies
这个 table 包含一个敌人列表(作为一个序列)以及一个冷却时间值,这是一个数字。
pairs
此函数遍历 table.
的 所有 键值对问题
你现在可能已经猜到了。 pairs(storage.enemies)
不仅遍历所有敌人(整数键),而且遍历冷却时间(字符串键 'cooldown'
),导致您尝试索引一个数字。
示例:
for key, value in pairs({'a', 'b', 'c', foo='bar'}) do
print(key, value)
end
会打印类似
的内容1 a
foo bar
3 c
2 b
请注意,顺序是完全随机的,取决于 Lua 如何决定在内部存储 table。
for key, value in ipairs({'a', 'b', 'c', foo='bar'}) do
print(key, value)
end
然而,应该打印:
1 a
2 b
3 c
因为 ipairs
,与 pairs
相反,它只会按顺序遍历整数键,直到达到第一个 nil
值。例如,对于 {1, 2, 3, nil, 5}
它只会迭代 1
、2
和 3
.