How to stop character from moving?

#1

Hello all, I hope that everything is well!

I’m working on a functionality that freezes the character while a dialogue box pops up. I am using Platypus for movement and this is what I tried:

  • I created variable: canmove

  • I add variable canmove to all of the movement related Platypus lines: if self.input_state[LEFT] and canmove then…

  • On init I set canmove to true

  • When the dialogue box pops up I set canmove to false

This all works just fine until I set canmove to true again. Once canmove is set to true again the character begins to move without any input being put in by the user. It only stops once input is put in again. This code is also quite laggy. I was wondering if there is a better and more efficient way to do this.

script.txt (4.9 KB)
Here is the script. (sorry about the sloppiness)
Thanks!

0 Likes

#2

Hi!

I’ve had a look at your script and it looks like things are a little complicated.

My advice would be that you try this simple script for movement:

 function init(self)
 	msg.post(".", "acquire_input_focus")
 	self.vel = vmath.vector3()
 end
 
function update(self, dt)
 	local pos = go.get_position()
 	pos = pos + (self.vel * dt)
 	go.set_position(pos)
 	self.vel.x = 0
 end
 
 function on_input(self, action_id, action)
 	
 	if action_id == hash("right") then
 		self.vel.x= 150
 		sprite.play_flipbook("#sprite", "walk")
 	elseif action_id == hash("left") then
 		self.vel.x= -150
 		sprite.play_flipbook("#sprite", "walk")
 	end
 	if action_id == hash("right") and action.released then
 		self.vel.x= 150
 		sprite.play_flipbook("#sprite", "idle")
 	elseif action_id == hash("left") and action. released then
 		self.vel.x= -150
		sprite.play_flipbook("#sprite", "idle")
	end
end
2 Likes

#3

Hello!
Thank you so much for taking the time to put together this script.
As of right now the movement script itself is working fine and also has some neat qualities like doublejump and walljump!
My main concern right now is the most efficient way in preventing movement then allowing it again.

Thanks so much though, I will definitely keep this in mind for future projects!

0 Likes

#4

Haven’t looked into things like this myself but the first thing that comes to mind is to alter the timestep https://defold.com/manuals/collection-proxy/?q=timestep#time-step

2 Likes

#5

Try this script
script.txt (3.7 KB)

0 Likes

#6

This is an excellent suggestion. Combine it with releasing input focus while paused and you’re good to go!

2 Likes

#7

This script work wonderfully! Thank you so much. What do you think the issue was?

0 Likes

#8

I didn’t realise you had that kind of control! I will definitely take a look at timesteps and see how I can utilise them!
Much thanks!

0 Likes