【问题标题】:Lua LOVE2D can't add more than one bullet to the world using bumpLua LOVE2D 不能使用凹凸向世界添加超过一颗子弹
【发布时间】:2020-03-18 20:36:41
【问题描述】:

我刚刚进入 LOVE2D 并制作平台游戏,但在尝试使用凹凸库时遇到了障碍。我有一个玩家可以射击子弹并将其添加到世界上,我只射击一次就很好,但是当我再次射击时,LOVE 给了我这个错误:

Error

libs/bump/bump.lua:619: Item table: 0x121b5a18 added to the world twice.

Traceback

[C:]: in function 'error'
libs/bump/bump.lua:619: in function 'add'
main.lua:39: in function 'update'
main.lua:118: in function 'update'
[C:] in function 'xpcall'

我将子弹添加到世界的过程是实例化子弹,将其添加到名为子弹的表中,然后循环遍历表将每个添加到世界中。我知道问题是它不会让我将相同的项目添加到世界中,所以我的问题是如何向世界添加多个项目符号而不会认为它们是相同的?

这是我更新项目符号的代码:

function UpdateBullet(dt)
    shootTimer = shootTimer - 1 * dt
    if shootTimer <= 0 then
        player.canShoot = true
    end

    if love.keyboard.isDown("z") and player.canShoot then
        -- instantiate it next to player and a bit up 
        newBullet = {x=player.x + player.width, y = player.y + 5}
        table.insert(bullets, newBullet)
        -- Width and height hardcoded for now
        for i, bullet in ipairs(bullets) do
            world:add(bullet, bullet.x, bullet.y, 10, 10)
        end    
        player.canShoot = false
        shootTimer = player.shootDelay
    end   

    for i, bullet in ipairs(bullets) do
        -- bullet speed and screen size also hardcoded rn, oops
        bullet.x = bullet.x + 250 * dt
        -- if bullet goes off screen, remove it
        if bullet.x > 600 then
            table.remove(bullets, i)
        end
    end
end

如果有任何帮助,我将不胜感激。提前致谢

【问题讨论】:

  • 在向世界添加项目符号时,为什么要遍历整个 bullets 数组?您可以(应该?)将 newBullet 添加到世界。
  • 我这样做是为了让我可以遍历表格并使用 for i, bullet in ipairs(bullets) do 块更新它们的位置
  • ** 并循环绘制它们

标签: lua love2d


【解决方案1】:
    for i, bullet in ipairs(bullets) do
        world:add(bullet, bullet.x, bullet.y, 10, 10)
    end

这部分代码从全局列表中添加项目符号。假设您在该列表中有 10 个项目符号。您添加 1 个新项目符号。然后将这 11 个项目符号添加到世界。但是您已经在上一次函数运行中将这 11 个项目符号中的 10 个添加到了世界中。

【讨论】:

  • 啊,我明白了,每次我按 z 时,它都会尝试再次添加所有这些。我删除了 for 循环,它现在可以工作了。谢谢!
猜你喜欢
  • 2017-09-19
  • 2018-10-30
  • 2022-12-31
  • 1970-01-01
  • 2015-04-09
  • 2022-01-05
  • 2012-09-30
  • 1970-01-01
  • 2012-04-30
相关资源
最近更新 更多