I was thinking maybe something like this
Code: Select all
table.remove(enemy)(ID)
I have no idea though :/
-Wolf
Code: Select all
table.remove(enemy)(ID)
Code: Select all
enemy.ID = nil
Code: Select all
for i = #enemy, 1, -1 do
enemy[i].ID = nil
end
Code: Select all
enemy = {}
function enemycreate(x,y,direction,id)
table.insert(enemy,{x=x,y=y,direction=direction,id=id,speed = 240,h = 32,w = 32})
end
function enemyupdate(dt)
for i,v in ipairs(enemy) do
if v.direction == "right" then
v.x = v.x + v.speed * dt
end
if v.direction == "left" then
v.x = v.x - v.speed * dt
end
end
end
function enemycollide(dt)
for i,v in ipairs(enemy) do
E_timedspeed = v.speed * dt
if E_mapCollide(v.x - E_timedspeed, v.y + 1) or B_mapCollide(v.x - E_timedspeed, v.y + v.h - 1) then
v.x = math.floor(v.x / tiledivisor) * tiledivisor
v.direction = "right"
v.x = v.x + 32
end
if E_mapCollide(v.x + E_timedspeed + v.w, v.y + 1) or B_mapCollide(v.x + E_timedspeed + v.w, v.y + v.h - 1) then
v.x = (math.floor((v.x + v.w + E_timedspeed) / tiledivisor) * tiledivisor) - v.w
v.x = v.x - 32
v.direction = "left"
end
end
end
function enemy_draw()
for i,v in ipairs(enemy) do
g.setColor(255,255,255,255)
g.draw(E, v.x, v.y, 0, 1, 1)
end
end
function enemy_die(dt)
for i,v in ipairs(enemy) do
if B_x > v.x and
B_x < v.x + v.w and
B_y > v.y and
B_y < v.y + v.h then
print "hyeap"
if v.id == 1 then
table.remove(enemy,1)
end
if v.id == 2 then
table.remove(enemy,2)
end
end
end
end
Code: Select all
if gamestate == "lvl 3" and
enemyloaded == false then
enemycreate(128,32,"right",1)
enemycreate(256,32,"left",2)
enemyloaded = true
end
Now niether remove :/Qcode wrote:Have you tried both of our solutions? Because that error sounds like something that would happen without using a reverse loop. Try my code if you haven't.
Code: Select all
for i = #enemy, 1, -1 do
if enemy[i].remove then
table.remove(enemy, i)
end
end
I'll try it, now what does all that mean? for i = #enemy, (this calls the table right? ) but then after that what does 1,-1 meanQcode wrote:I'd do the remove function like this:Is that what you're looking for?Code: Select all
for i = #enemy, 1, -1 do if enemy[i].remove then table.remove(enemy, i) end end
Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 14 guests