错误main.lua:98:尝试索引本地'e'(数字值)

时间:2018-12-06 01:51:33

标签: lua love2d

我不断收到此错误消息,说我尝试使用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

1 个答案:

答案 0 :(得分:1)

首先

敌人的复数是敌人,而不是敌人;)

storage.enemies

此表包含一个敌人列表(按顺序排列)以及一个冷却时间值,该值是一个数字。

pairs

此函数遍历表的所有键/值对。

问题

您现在可能已经猜到了。 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如何决定在内部存储表。

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},它将仅迭代123