Enemy not getting deleted

I’m trying to have my enemies play a short animation when they die before deleting the game object so they disappear, but currently, the moving enemy (one moves one doesn’t, unrelated issue) will sometimes not get deleted after the animation and will just return to normal, except is just unkillable now. I added the print to see if it even enters the function but when this happens it appears it doesn’t.

  sprite.play_flipbook("#sprite", hash("Poof"), function()
    	go.delete()
    	print("deleted")
  end)

Here is my full enemy.collision script

local max_enemyHealth = 3 -- creates a health variable
local TILE_SIZE = 16
print(enemyHealth)

function init(self)
	self.correction = vmath.vector3() -- correction vector
	self.enemyHealth = max_enemyHealth
end

function update(self, dt)
	self.correction = vmath.vector3() -- correction vector
end

function on_message(self, message_id, message, sender)
	
	if message_id == hash("contact_point_response") then --checks for a collision message sent by the game
		if message.other_group == hash("objects") or message.other_group == hash("signs") then
			if message.distance > 0 then 
				local proj = vmath.project(self.correction, message.normal * message.distance) -- Combine the accumulated correction with the penetration vector
				if proj < 1 then
					local comp = (message.distance - message.distance * proj) * message.normal -- Calculate how much the player will have to be moved to compensate
					go.set_position(go.get_position() + comp) -- Add the compensation onto the player's position to put them in the right location
					self.correction = self.correction + comp -- Accumulate the correction applied
				end
			end
		end

		if message.other_group == hash("sword") and self.invulnerable ~= true then
			enemyHealthChange(self, -1)
			local pos = go.get_position()
			-- set a pushback direction based on the collision normal
			local to = pos + message.normal * 30
			-- knockback animation, then continue moving
			go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_OUTQUAD, 0.1, 0, move)
			self.invulnerable = true
			local j
			j = 0
			timer.delay(0.1, true, function(self,handle,time_elapsed)
				if j<5 or j == 5 then 
					go.animate("#sprite", "tint", go.PLAYBACK_ONCE_PINGPONG, vmath.vector4(1, 1, 1, 0), go.EASING_LINEAR, 0.05,0,anim_done)
					j = j+1
				elseif j > 5 then
					timer.cancel(handle)
				end
			end)
			timer.delay(1, false, function()
				--reset invulnerability
				self.invulnerable = false
			end)
		end
	end
end

function enemyHealthChange(self, hearts)
	self.enemyHealth = self.enemyHealth + hearts
	if self.enemyHealth == 0 then
		msg.post("#Enemy", "death")
		sound.play("sfx#Enemy_Death") -- plays the enemy death sfx
		sprite.play_flipbook("#sprite", hash("Poof"), function()
			go.delete()
			print("deleted")
		end)
	elseif self.enemyHealth > max_enemyHealth then
		self.enemyHealth = max_enemyHealth
		
	elseif self.enemyHealth < 0 then
		self.enemyHealth = 0
		
	end

end

Here is my enemy.script

go.property("speed", 40) -- sets the speed that the player will move
direction = "down" -- sets down as the default direction
local STATE_WALKING = hash("walking")
local STATE_IDLE = hash("idle")
walk = true
local dataStorage = require("main.Data Storage") 

function init(self)
	self.vel = vmath.vector3() -- indicates velocity, starts at 0
	self.enemyHealth = dataStorage.enemyHealth.enemy_value -- creates a health variable
end

function update(self, dt)
	local pos = go.get_position() -- gets the current position and stores it
	pos = pos + self.vel * dt -- adds the velocity to the position
	go.set_position(pos) -- sets the position of the game object to the new position
	
	if walk == true then
		random_move = math.random (1,5)
		walk = false
		if random_move == 1 then -- checks the input
			self.direction = "up"
			self.vel.y = self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("U_Walk")) -- Changes Animation to walk

		elseif random_move == 2 then -- checks the input
			self.direction = "down"
			self.vel.y = -self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("D_Walk")) -- Changes Animation to walk

		elseif random_move == 3 then -- checks the input
			self.direction = "left"
			self.vel.x = -self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("L_Walk")) -- Changes Animation to walk

		elseif random_move == 4 then -- checks the input
			self.direction = "right"
			self.vel.x = self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("R_Walk")) -- Changes Animation to walk

		elseif random_move == 5 then
			self.vel.y = 0
			self.vel.x = 0
		end

		if self.vel.x == 0 and self.vel.y == 0  then
			self.state = STATE_IDLE
			if self.direction == "up" then -- checks what direction the player is facing
				sprite.play_flipbook("#sprite", hash("Log_U_Idle")) -- Sets the animation to idle

			elseif self.direction == "down" then -- checks what direction the player is facing
				sprite.play_flipbook("#sprite", hash("Log_D_Idle"))  -- Sets the animation to idle

			elseif self.direction == "left" then -- checks what direction the player is facing
				sprite.play_flipbook("#sprite", hash("Log_L_Idle"))  -- Sets the animation to idle

			elseif self.edirection == "right" then -- checks what direction the player is facing
				sprite.play_flipbook("#sprite", hash("Log_R_Idle"))  -- Sets the animation to idled
			end
		end

		if message_id == hash("contact_point_response") then
			if message.other_group == hash("objects") then
				walk = true
			end
		else
			timer.delay(1, false, function()
				--reset invulnerability
				walk = true
			end)
		end
		
	end
	
end

function on_message(self, message_id, message, sender)
	if message_id == hash("death") then
		self.vel.x = 0
		self.vel.y = 0
	end
end

I’ve only skimmed the code but do you prevent other flipbook animations from happening once death is imminent? Feels like it could be overwritten. Since you only trigger the animation at health == 0 it makes sense that it would be unkillable afterwards.

2 Likes

It happens for the moving enemy (and most probably not for a stationary one), because when you are moving it - you change animation, so the “poof” animation might not be finished (and therefore a callback is not called), because you changed the animation meanwhile. Try to add some flag, that marks the object dead (so should not be moving, etc) before even starting this animation.

P. S. If things get more complicated you can try out to use some Lua Finite State Machine :wink:

1 Like