Movement issues (SOLVED)

Heya, I’ve got this issue I’ve been trying to solve for hours. I went through most of the topics regarding movement, but I haven’t really managed to come up with a solution.
At first what I wanted to do was make my player sprite move when I touch the screen, but instead I went with a simpler solution and created some gui buttons to control it.
Left and right seem to be working alright, but I can’t get him to come down to its original position when I press jump, plus if I tap jump and then slide my my thumb across the left or right button simultanously then it just starts moving in the air, in either direction.
I’ve decided to use on_message instead of input as it seemed to be easier.
I really started using defold only a short while ago, same with lua so any help is appreciated.

   function init(self)
	msg.post(".", "acquire_input_focus") 
	self.vel = vmath.vector3()     
end

function update(self, dt)
	local anim = hash("player_idle")

	if self.vel.x > 0 then
		anim = hash("player_walk")
	elseif self.vel.x < 0 then
		anim = hash("player_walk")
	elseif self.vel.y > 0 then
		anim = hash("player_jump")
	elseif self.vel.y < 0 then
		anim = hash("player_jump")
	end

	if anim ~= self.current_anim then
		msg.post("#player", "play_animation", { id = anim })
		self.current_anim = anim
	end
	
	local pos = go.get_position() 
	pos = pos + self.vel * dt 
	go.set_position(pos) 
	
	self.vel.x = 0 
	self.vel.y = 0
end

function on_message(self, message_id, message, sender)
	if message_id == hash("move_right") then
		self.vel.x = -150
	elseif message_id == hash("move_left") then
		self.vel.x = 150
	elseif message_id == hash("jump") then
		self.vel.y = 1500
	elseif message_id == hash("jump_down") then
		self.vel.y = -1500
	end
end

The issue is in these lines:

self.vel.x = 0
self.vel.y = 0

Your character has no sense of acceleration and you set all of their velocity to zero. You need a gravity vector to constantly apply that downward acceleration. Here is a good video reference from a guy who makes a lot of really good videos over game and graphics programming.

There is also a Defold library written by @britzl called Platypus that I am personally using and enjoying in my game.

2 Likes

Also, always use on_input as often as you can. I use on_message in my game, but that is because I use a completely different script to pass on input messages to their relevant game objects.

2 Likes

Many thanks mate, I managed to fix it. :grin:
I’ll have a look at that library as well, seems like something I’d use.
Thanks again!

2 Likes