For loop 对于带有lö的lua中的循环;ve2D,删除变量
我对编码有点初级,而且我的英语不是很好,我希望我能把我的问题说清楚: 因此,我的代码中有一个For loop 对于带有lö的lua中的循环;ve2D,删除变量,for-loop,lua,love2d,For Loop,Lua,Love2d,我对编码有点初级,而且我的英语不是很好,我希望我能把我的问题说清楚: 因此,我的代码中有一个for-循环,其中有2个if,在每个if中,我查看某个东西是真是假,如果是真的,我删除循环的一部分。就像这样: for n=#Test,1, -1 do if Test[n].delete == true then table.remove(Test, n ) end if Test[n].y > 0 then table.remove(Test, n
for
-循环,其中有2个if
,在每个if
中,我查看某个东西是真是假,如果是真的,我删除循环的一部分。就像这样:
for n=#Test,1, -1 do
if Test[n].delete == true then
table.remove(Test, n )
end
if Test[n].y > 0 then
table.remove(Test, n )
end
end
有点像这样,我的问题是,如果第一个if
makeTest[n]
被删除,那么游戏会在下一个if
崩溃,因为Test[n]
不再存在。我通过为-循环创建2个,如果
,则每个循环一个,解决了这个问题
但是我看到有人没有为
-循环使用2就完成了它,它没有崩溃,我试图找出哪里出了问题,但我找不到它
如果有人能发现我写的东西有什么问题,我会很感激的
因此,我的代码在第9行出现了问题,如果条件满足,我将表。删除(listedirs,n)
,然后在第17行,当代码尝试再次查找以测试它时,它会出现错误:
for n=#ListeTirs,1, -1 do
if ListeTirs[n].Type == "Gentils"
then local nAliens
for nAliens=#ListeAliens,1,-1 do
if
Collide(ListeTirs[n], ListeAliens[nAliens]) == true
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n )
ListeAliens[nAliens].Supprime = true
table.remove(ListeAliens, nAliens)
end
end
end
if
ListeTirs[n].y < 0 - ListeTirs[n].HauteurMoitie or ListeTirs[n].y > Hauteur + ListeTirs[n].HauteurMoitie or ListeTirs[n].x > Largeur + ListeTirs[n].LargeurMoitie or ListeTirs[n].x < 0 - ListeTirs[n].LargeurMoitie
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n)
end
end
对于n=#listedirs,1,-1 do
如果ListIrs[n]。类型==“Gentils”
然后是当地的纳利恩斯
对于nAliens=#listenes,1,-1 do
如果
碰撞(李斯特[n],李斯特[nAliens])==真
然后
ListIrs[n]。Supprime=true
表.删除(列表,n)
Listenes[nAliens]。Supprime=true
表.删除(列表、列表)
终止
终止
终止
如果
李斯特[n].y<0-李斯特[n].HauteurMoitie或李斯特[n].y>Hauteur+李斯特[n].HauteurMoitie或李斯特[n].x>Largeur+Listirs[n].LargeurMoitie或李斯特[n].x<0-李斯特[n].LargeurMoitie
然后
ListIrs[n]。Supprime=true
表.删除(列表,n)
终止
终止
我希望这是清楚的,我可以张贴整个代码,但我觉得没有必要,如果是的话,我会添加它
谢谢:)正是我所需要的,非常清楚的答案,非常感谢:)埃戈尔方法的另一种选择是使用elseif
语句。您的示例的问题是,在一个循环中可以删除两个项,这将导致错误。
for n=#Test,1, -1 do
local should_be_removed
-- just mark for deletion
if Test[n].delete = true then
should_be_removed = true
end
-- just mark for deletion
if Test[n].y > 0 then
should_be_removed = true
end
-- actually delete (at the very end of the loop body)
if should_be_removed then
table.remove(Test, n )
end
end