如何让抛射物出现在爱情的一个地方
How to have a projectile appear in a single spot in love
我目前正在 Lua 中使用 Love2d 引擎创建一个游戏,我想知道是否有可能使类似弹丸的东西出现在一个地方。就像英雄向前方 50 像素的敌人发射火焰柱一样。理想情况下,火焰柱会出现,伤害敌人,然后消失,这一切都可能在一帧内发生。到目前为止,我的代码如下:
local function addProj()
local speed = dt
for i, projectile in ipairs(proj) do
if playerBack == true then
projectile.y = projectile.y - 250*speed
if projectile.y < player.y - 150 then
table.remove(proj,i)
end
elseif playerForward == true then
projectile.y = projectile.y + 250*speed
if projectile.y > player.y + 150 then
table.remove(proj,i)
end
elseif playerLeft == true then
projectile.x = projectile.x - 250*speed
if projectile.x < player.x - 150 then
table.remove(proj,i)
end
elseif playerRight == true then
projectile.x = projectile.x + 250*speed
if projectile.x > player.x + 150 then
table.remove(proj,i)
end
end
end
end
上面的代码现在只是一种变通方法,但它基本上是将射弹沿给定方向移动一定距离,然后使其消失。感谢您提供的所有帮助!
好的,所以最好的方法是设置世界回调并测试碰撞。有很好的教程、示例和文档。本质上,这将允许您做的是让您的程序感知物理对象的碰撞。
page for the creation of the callbacks
如果你想像某些人对基础物理所做的那样手动完成,你可以将所有对象放在 table 中,并且每次更新你都会检查 table 以查看如果它们的 x 或 y 值匹配,这显然在大型游戏中会减慢游戏速度。
我目前正在 Lua 中使用 Love2d 引擎创建一个游戏,我想知道是否有可能使类似弹丸的东西出现在一个地方。就像英雄向前方 50 像素的敌人发射火焰柱一样。理想情况下,火焰柱会出现,伤害敌人,然后消失,这一切都可能在一帧内发生。到目前为止,我的代码如下:
local function addProj()
local speed = dt
for i, projectile in ipairs(proj) do
if playerBack == true then
projectile.y = projectile.y - 250*speed
if projectile.y < player.y - 150 then
table.remove(proj,i)
end
elseif playerForward == true then
projectile.y = projectile.y + 250*speed
if projectile.y > player.y + 150 then
table.remove(proj,i)
end
elseif playerLeft == true then
projectile.x = projectile.x - 250*speed
if projectile.x < player.x - 150 then
table.remove(proj,i)
end
elseif playerRight == true then
projectile.x = projectile.x + 250*speed
if projectile.x > player.x + 150 then
table.remove(proj,i)
end
end
end
end
上面的代码现在只是一种变通方法,但它基本上是将射弹沿给定方向移动一定距离,然后使其消失。感谢您提供的所有帮助!
好的,所以最好的方法是设置世界回调并测试碰撞。有很好的教程、示例和文档。本质上,这将允许您做的是让您的程序感知物理对象的碰撞。
page for the creation of the callbacks
如果你想像某些人对基础物理所做的那样手动完成,你可以将所有对象放在 table 中,并且每次更新你都会检查 table 以查看如果它们的 x 或 y 值匹配,这显然在大型游戏中会减慢游戏速度。