I have an enemy that creates small spheres that circle around the enemy and are thrown at the hero. However, when I have 2 enemies, only one of them will do the proper attack.
local enemy = ...
local game = enemy:get_game()
local can_shoot = true
local sprite = nil
local nb_bats_created = 0
local nb_to_create = 0
local raintimer = nil
function enemy:on_created()
-- Set properties.
self:set_life(4); self:set_damage(6); self:set_hurt_style("normal")
self:set_pushed_back_when_hurt(true); self:set_push_hero_on_sword(false)
self:set_traversable(true)
-- Create sprite if necessary, etc.
sprite = self:get_sprite()
if not sprite then sprite = self:create_sprite("enemies/more/dark/rainbow") end
function sprite:on_animation_finished(animation)
if animation == "looking" then sprite:set_animation("stopped") end --***
end
-- Start enemy.
self:restart()
end
local function go_hero()
local map = enemy:get_map()
local hero = map:get_hero()
if enemy:get_distance(hero) < 120 then
local sprite = enemy:get_sprite()
sprite:set_animation("walking")
local movement = sol.movement.create("target")
movement:set_speed(64)
movement:start(enemy)
else
local sprite = enemy:get_sprite()
sprite:set_animation("walking")
local movement = sol.movement.create("random")
movement:set_speed(64)
movement:start(enemy)
end
end
function enemy:attack()
if jumping or attacking then
return
end
if cancel_next_attack then
cancel_next_attack = false
return
end
attacking = true
sprite:set_animation("shooting")
local prefix = "bat" .. "_bat_"
nb_to_create = 6
function repeat_throw_bat()
sol.audio.play_sound("lamp")
nb_bats_created = nb_bats_created + 1
local son_name = prefix .. nb_bats_created
local son = enemy:create_enemy{
name = son_name,
breed = "more/dark/rainattack",
x = 0,
y = 0,
layer = 0,
}
if math.random(6) == 1 then
son:set_treasure("magic_flask", 1)
end
son:go_circle(enemy)
local go_hero_delay = 2000 + (nb_to_create * 150)
sol.timer.start(son, go_hero_delay, function() son:go_hero() end)
nb_to_create = nb_to_create - 1
if nb_to_create > 0 then
raintimer = sol.timer.start(enemy:get_map(), 350, repeat_throw_bat)
else
attacking = false
attack_scheduled = false
if not vulnerable then
sol.timer.start(2000, function()
go_hero()
end)
else
cancel_next_attack = false
end
end
end
enemy:stop_movement()
enemy:get_sprite():set_direction(0)
repeat_throw_bat()
end
function enemy:on_hurt()
nb_to_create = 0
if raintimer ~= nil then
raintimer:stop()
end
enemy:get_map():remove_entities("bat" .. "_bat_")
end
function enemy:on_dying()
nb_to_create = 0
if raintimer ~= nil then
raintimer:stop()
end
enemy:get_map():remove_entities("bat" .. "_bat_")
end
function enemy:on_restarted()
local map = enemy:get_map()
local hero = map:get_hero()
go_hero()
can_shoot = true
sol.timer.start(enemy, 100, function()
local hero_x, hero_y = hero:get_position()
local x, y = enemy:get_center_position()
if can_shoot then
if enemy:get_distance(hero) < 150 then
attack()
can_shoot = false
sol.timer.start(enemy, 6666, function()
can_shoot = true
end)
end
end
return true -- Repeat the timer.
end)
end
function enemy:on_movement_changed(movement)
local direction4 = movement:get_direction4()
local sprite = self:get_sprite()
sprite:set_direction(direction4)
end
Some of your state variables, like 'attacking', are not local.