in love.update(dt)
Code: Select all
for k,enemy in pairs(enemies) do
if enemy.x == player.x and enemy.y == player.y then
table.remove(enemies,enemy)
end
end
Code: Select all
for k,enemy in pairs(enemies) do
if enemy.x == player.x and enemy.y == player.y then
table.remove(enemies,enemy)
end
end
Code: Select all
table.remove(list, index)
Code: Select all
for i = #list, 1, -1 do
if list[i] == search_item then
table.remove(list, i)
-- break -- if you want to remove just 1 element
end
end
Code: Select all
for k, v in pairs(t) do
if v == search_item then
t[k] = nil
-- break -- if you want to remove just 1 element
end
end
ivan wrote:"table.remove" is used for numerically-indexed tables and works like so:Where "list" is a numerically indexed table and "index" is the numeric index of the element you want to remove.Code: Select all
table.remove(list, index)
Removing more than 1 element from a numerically indexed table during iteration is always done in reverse because it modifies the indices of the following elements:If your table is not-numerically indexed you can always write:Code: Select all
for i = #list, 1, -1 do if list[i] == search_item then table.remove(list, i) -- break -- if you want to remove just 1 element end end
Code: Select all
for k, v in pairs(t) do if v == search_item then t[k] = nil -- break -- if you want to remove just 1 element end end
Code: Select all
for i=#enemies, 1,-1 do
if enemies[i] == player.x or player.y then
table.remove(enemies,i)
end
end
Code: Select all
for i=#enemies, 1,-1 do
if enemies[i].x == player.x and enemies[i].y == player.y then
table.remove(enemies,i)
end
end
Code: Select all
for i=#enemies, 1,-1 do
if distance(player, enemies[i]) < threshold then
table.remove(enemies,i)
end
end
I litterally copy/paste your code inside love.update(dt) and it doesn't do anything...ivan wrote:try:also, this code may not be very robust, you probably want to check the distance between the player and the enemyCode: Select all
for i=#enemies, 1,-1 do if enemies[i].x == player.x and enemies[i].y == player.y then table.remove(enemies,i) end end
Code: Select all
for i=#enemies, 1,-1 do if distance(player, enemies[i]) < threshold then table.remove(enemies,i) end end
Code: Select all
function love.load()
enemies =
{
{
image = love.graphics.newImage("r_demon.gif"),
x = 200, y = 300, speed = 80, heading = 0,
hw = 16, hh = 16
},
{
image = love.graphics.newImage("b_demon.gif"),
x = 300, y = 300, speed = 60, heading = 0,
hw = 16, hh = 16
}
}
end
Code: Select all
pt.x == pt2.x and pt.y == pt2.y
Yep, add the function "distance" in there and it should work.I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
i did this...in love.update(dt)ivan wrote:Keep in mind that when you compare numbers like so:is not robust because enemies move at discrete steps (speed*dt).Code: Select all
pt.x == pt2.x and pt.y == pt2.y
Therefore, an enemy may be in the same position as the player between frames (so to speak).
Checking the "distance" instead is a starting point for an actual solution.
Yep, add the function "distance" in there and it should work.I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
Code: Select all
for k,enemy in pairs(enemies) do
distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
end
for i=#enemies, 1,-1 do
if distance then
table.remove(enemies.i)
end
end
You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.BluBillz wrote:i did this...in love.update(dt)ivan wrote:Keep in mind that when you compare numbers like so:is not robust because enemies move at discrete steps (speed*dt).Code: Select all
pt.x == pt2.x and pt.y == pt2.y
Therefore, an enemy may be in the same position as the player between frames (so to speak).
Checking the "distance" instead is a starting point for an actual solution.
Yep, add the function "distance" in there and it should work.I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?But I get error...saying: bad argument #1 to 'remove' (table expected, got nil)Code: Select all
for k,enemy in pairs(enemies) do distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2) end for i=#enemies, 1,-1 do if distance then table.remove(enemies.i) end end
You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.
Because "or" doesn't work the way you intend to use it. "Or" is a logical operator, so your if basically means "if player and enemy are at the same x coordinate or player.y exists" which is always true. Someone else posted a solution above and hinted at instead using the distance (because enemies rarely exactly hit your exact spot)BluBillz wrote:You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.
Well i did fix that...but all it does now is the enemies spawn instantly...then delete instantly...Not getting the players x and y positions..why would this be?
Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 3 guests