Lua LOVE2D 不能使用凹凸向世界添加超过一颗子弹
Lua LOVE2D can't add more than one bullet to the world using bump
我刚开始接触 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'
我向世界添加子弹的过程是实例化子弹,将其添加到名为 bullets 的 table,然后遍历 table 将每个子弹添加到 world.I 明白问题在于它不会让我将相同的项目添加到世界中,所以我的问题是如何在不认为它们相同的情况下向世界中添加多颗子弹?
这是我更新项目符号的代码:
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
如果有任何帮助,我将不胜感激。提前致谢
for i, bullet in ipairs(bullets) do
world:add(bullet, bullet.x, bullet.y, 10, 10)
end
您的这部分代码添加了全局列表中的项目符号。假设您在该列表中有 10 个项目符号。您添加 1 个新项目符号。然后将这 11 颗子弹添加到世界中。但是您已经在上一个函数中向世界添加了这 11 颗子弹中的 10 颗 运行。
我刚开始接触 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'
我向世界添加子弹的过程是实例化子弹,将其添加到名为 bullets 的 table,然后遍历 table 将每个子弹添加到 world.I 明白问题在于它不会让我将相同的项目添加到世界中,所以我的问题是如何在不认为它们相同的情况下向世界中添加多颗子弹?
这是我更新项目符号的代码:
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
如果有任何帮助,我将不胜感激。提前致谢
for i, bullet in ipairs(bullets) do
world:add(bullet, bullet.x, bullet.y, 10, 10)
end
您的这部分代码添加了全局列表中的项目符号。假设您在该列表中有 10 个项目符号。您添加 1 个新项目符号。然后将这 11 颗子弹添加到世界中。但是您已经在上一个函数中向世界添加了这 11 颗子弹中的 10 颗 运行。