Page 1 of 1
Why doesn't this death fucntion work?
Posted: Fri Jun 19, 2015 8:05 pm
by Wrathguy78
I just want it so if an enemy is in the same position with the player it kills them I made this code function and it doesn't work.
Code: Select all
function enemy_player_collide()
for i,v in ipairs(enemy) do
for ia, va in ipairs(player) do
if va.x == v.x and
va.y == v.y then
va.x = 0
end
end
end
end
Re: Why doesn't this death fucntion work?
Posted: Fri Jun 19, 2015 9:23 pm
by rok
Here you have a short example:
Code: Select all
local players = {
{x = 5, y = 5}
}
local enemies = {
[1] = {x = 5, y = 5, enabled = true},
[2] = {x = 6, y = 6, enabled = true},
[3] = {x = 3, y = 3, enabled = true}
}
function collide()
for _,player_value in pairs(players) do
for enemy_key, enemy_value in pairs(enemies) do
if player_value.x == enemy_value.x and player_value.y == enemy_value.y then
-- kill the enemy?
enemies[enemy_key].enabled = false
print("Killed the enemy "..enemy_key)
print("There are "..count_enabled().." enemies left to defeat.")
end
end
end
end
function count_enabled()
local counter = 0 -- count how many enemies have the flag set to true
for enemy_key, enemy_value in pairs(enemies) do
if enemies[enemy_key].enabled == true then
counter = counter + 1
end
end
return counter
end
collide()
I don't know how your enemy table looks like. You're changing the
va value not the value at the index/key in the table.