【发布时间】: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块更新它们的位置 -
** 并循环绘制它们