Flip_book: complete_function not executed after rapid tapping (SOLVED)

Dear,

I’m using the “play_flipbook” instruction for my sprite animation, the animation is about a character swinging its sword. So when the player hits the keyboard “X” the following happen:
1- My game put can_input = false (it’s a flag to stop all input till my sprite finishes the animation)
2- I execute the animation using sprite.play_flipbook(url,id, COMPLETE_FUNCTION)
3- COMPLETE_FUNCTION: among other things, it puts can_input=true to give the user access to input again

I can see it’s working just fine!

THE PROBLEM: when I hit the “X” rapidly (nervously), the “COMPLETE_FUNCTION” is not executed after few seconds and “can_input remains false” which make me lose user input!
This is not suppose to happen because I downloaded the slasher template, and I can see it’s working even with rapid tapping on “X” keyboard

QUESTION: What am I doing wrong? (in the slasher template I couldn’t understand the code and where should I start)

Hope I made myself clear and someone can help me out

Regards
Riad

Can you please share your code? Can you also check the console for any errors?

Thanks a lot for the answer

I have included the code in the attachment. FYI, I’m basing my trial on the asset: “True Tile Collisions”(https://defold.com/assets/truetilecollisions/). I’m using Defold 1.2.174

I have added some print at the right spot. when you hit rapidly “X” you should see after few seconds the problem.

Example.zip (307.1 KB)

Yes, I’m able to reproduce the problem. It is definitely a problem in the state handling logic, but I didn’t have the time to debug it. You need to rethink the design somewhat. In general it is not recommended to poll for state changes like you do in update(). If possible you should write reactive logic, ie code which reacts to state changes or events, instead of polling for changes.

I modified your hero.script to only deal with idle and attack states and with the setup below it is impossible to reproduce the problem you had with the original code:

require "true_tile_collision.true_tile_collision"
require "objects.param"

function init(self)
	msg.post(".", "acquire_input_focus")				--Enable inputs for player
	local url = msg.url()								--Physics system need object url

	-------------------------------
	-- true_tile_collision ASSET --
	-- requires: (self, URL, collisionTilemap, collisionLayer, tileSize, maxHorizontalSpeed, jumpSpeed, gravity)
	init_physics(self, url, "level1:/background#level1tiles", "collision", 32, 1.5, 5, 0.2) 
	set_hitbox(self, 8, -8, 28, 0)
	------------- end -------------

	self.cur_state = nil
	self.can_input = true
end

function update(self, dt)
	--PHYSICS
	physics_update_platformer_block(self, dt)
end

local function idle(self)
	self.cur_state = state.idle
	sprite.play_flipbook("#sprite", IDLE)
end

local function attack2(self)
	self.cur_state = state.sttack2
	sprite.play_flipbook("#sprite", ATTACK2, function()
		self.can_input = true
		idle(self)
	end)
end

function on_input(self, action_id, action)
	if not self.can_input then
		return
	end

	if action_id == ATTACK2 then
		if action.pressed and self.cur_state ~= state.attack2  then
			self.can_input = false
			attack2(self)
		end
	end
end

2 Likes

Thanks a million!

It really solved my problem. Now it’s working!

Regards
Riad