Why doesn't this death fucntion work?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

Why doesn't this death fucntion work?

Post 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
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Why doesn't this death fucntion work?

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 141 guests