Input issues while trying to "stun" main character (SOLVED)

Greeting friends, it’s question time again…
I’m trying to stun my main character briefly when he gets hit by a “danger” object. This is my best effort so far:

I have two problems that I’m struggling to figure out. 1. My stun animation is not playing all the way through. 2. I haven’t found an effective way to temporarily disable and then re-enable input.

Here’s the relevant part of my update function:

function update(self, dt)
	local p = go.get_position()
	if self.got_hit then
		self.counter = self.counter + dt
		msg.post("#collisionobject", "disable")
		if self.facing == "r" then
			msg.post("#sprite","play_animation", {id = hash("animstunright")})
		else
			msg.post("#sprite","play_animation", {id = hash("animstunleft")})
		end
		p = p -- freeze
		if self.counter >= self.stun_time then
			self.got_hit = false
			self.counter = 0	
			msg.post("#collisionobject", "enable")
			if self.facing == "r" then
				msg.post("#sprite","play_animation", {id = hash("animright")})
			else
				msg.post("#sprite","play_animation", {id = hash("animleft")})
			end	
		end
		go.set_position(p)
	else...
  1. The Animation issue…
    Sorry guys just figured this one out, moved the play_animation out of update()

  2. The input issue…
    I want input for the player to stop completely during self.stun_time. At first I tried putting ‘msg.post(".", “release_input_focus”)’ right after ‘if self.got_hit’ and then ‘msg.post(".", “acquire_input_focus”)’ inside ‘if self.counter >= self.stun_time’ to get input going again. This first part worked. All movement stopped, but some really funky things happened with the player movement, when he’d come out of being stunned if you pressed any of the usual control buttons while he was stunned. He’d fly in a random direction and you couldn’t get back control for a bit. I set up a print test to the console and these weird movements definitely happened after coming out of the stun time. It’s strange to me that input focus isn’t restored straight away. Is there some other way around this? I scrapped that and moved on to the code as is above. Problem is the player is still able to wiggle left and right. I tried to stop that. In on_input, I added the qualifier, “and not self.got_hit”, to the if action_id == hash(“left”) and right segments of my code, but this created the same weird movements if you fiddled with the buttons while stunned. Send help please. :grin:

2 - Does the problem only happen when you are holding down a key when the stun ends? If that’s the case it’s because your player script won’t get the key pressed event (because its input was disabled), but it will get the release event. Depending on how you are handling input this could cause the player to move constantly until you press and release the same button again. Instead of disabling input you can use a self.is_stunned value and condition your movement code on that.

1 Like

I agree with Ross. It sounds as if your on_input function logic is dependent upon getting both the pressed and released events and when it only gets the released event it doesn’t behave as you want to. Please share your on_input function if you can’t figure out what’s going on.

1 Like

Thanks that’s exactly what’s happening. My on_input is reliant on release events at the moment so I’m probably going to have to find a different way to code it

Yep, that’s right. This is an example of what my whole on input looks like

if action_id == hash("up") then
        if action.pressed then
        	self.up = true
        elseif action.released then
        	self.up = false
-- etc...

Then we jump to update

if self.up then
			p.y = p.y + self.speed * dt
			if p.y >= screen_height - 35 then
				self.up = false
			end
		end
-- etc...

I think I’m going to have to change the whole thing, because it wont work without the release events with the way that I have it setup. I found a thread What is the most elemental way to move an Sprite in four directions? where you and PKeod give different examples of how to implement input, some of which at a cursory glance look like they don’t require release events, so I’ll try out one of those options, cheers :+1:

1 Like

Hmm, it’s probably a good idea to reset all stored input state (such as self.up) when you release_input_focus.

1 Like

You are a genius! Yessss that worked. Thanks! Saved me from having to re-write the whole thing

1 Like