Whenever I move my character the run anim keeps playing

Whenever I move my character the run anim keeps playing even when I dont move, but when I do it freezes. This is my code

function init(self) 
	msg.post(".","acquire_input_focus")
	self.runSpeed = 50
	self.curAnim = "idle"
	msg.post("#sprite", "play_animation", { id=hash("idle") })
	self.speed = vmath.vector3()
	particlefx.play("#playerparticle")
end
function update(self, dt)
	local pos = go.get_position()
	if self.speed.x ~= 0 then    
		pos = pos + self.speed * dt
		go.set_position(pos)
		if self.curAnim ~= "run" then
			msg.post("#sprite", "play_animation", { id=hash("run") })
			self.curAnim = "run"
			self.curAnim = "idle"
		end
	elseif self.curAnim ~= "idle" then
		msg.post("#sprite", "play_animation", { id=hash("idle") })
		self.curAnim = "idle"
	end
	self.speed = vmath.vector3()
end
function on_input(self, action_id, action)
	if action_id == hash("MOVE_RIGHT") then
		self.speed.x = self.runSpeed
		sprite.set_hflip("#sprite", false)
	end    
	if action_id == hash("MOVE_LEFT") then
		self.speed.x = self.runSpeed * -1
		sprite.set_hflip("#sprite", true)
	end        
end

This doesn’t look right. Can you spot the error?

None of that is needed?

You are setting the self.curAnim variable twice. First you set it to “run” and immediately afterwards to “idle”. You should remove the line where you set it to “idle”

Okay, but now it does the same thing with the idle animation

function init(self) 
	msg.post(".","acquire_input_focus")
	self.runSpeed = 50
	self.curAnim = "idle"
	msg.post("#sprite", "play_animation", { id=hash("idle") })
	self.speed = vmath.vector3()
	particlefx.play("#playerparticle")
end
function update(self, dt)
	local pos = go.get_position()
	if self.speed.x ~= 0 then    
		pos = pos + self.speed * dt
		go.set_position(pos)
		msg.post("#sprite", "play_animation", { id=hash("idle") })
		if self.curAnim ~= "run" then
			msg.post("#sprite", "play_animation", { id=hash("run") })
			self.curAnim = "run"
		end
	end
	self.speed = vmath.vector3()
end
function on_input(self, action_id, action)
	if action_id == hash("MOVE_RIGHT") then
		self.speed.x = self.runSpeed
		sprite.set_hflip("#sprite", false)
	end    
	if action_id == hash("MOVE_LEFT") then
		self.speed.x = self.runSpeed * -1
		sprite.set_hflip("#sprite", true)
	elseif action_id == hash("MOVE_LEFT") then
		self.speed.x = self.runSpeed * 0
		msg.post("#sprite", "play_animation", { id=hash("idle") })
			sprite.set_hflip("#sprite", true)
		end        
	end        

But you’ve changed the entire update() function from when you last posted here! You should remove only one line!

This is what it is supposed to look at (I haven’t tested it though):

function init(self) 
	msg.post(".","acquire_input_focus")
	self.runSpeed = 50
	self.curAnim = "idle"
	msg.post("#sprite", "play_animation", { id=hash("idle") })
	self.speed = vmath.vector3()
	particlefx.play("#playerparticle")
end

function update(self, dt)
	local pos = go.get_position()
	if self.speed.x ~= 0 then    
		pos = pos + self.speed * dt
		go.set_position(pos)
		if self.curAnim ~= "run" then
			msg.post("#sprite", "play_animation", { id=hash("run") })
			self.curAnim = "run"
		end
	elseif self.curAnim ~= "idle" then
		msg.post("#sprite", "play_animation", { id=hash("idle") })
		self.curAnim = "idle"
	end
	self.speed = vmath.vector3()
end

function on_input(self, action_id, action)
	if action_id == hash("MOVE_RIGHT") then
		self.speed.x = self.runSpeed
		sprite.set_hflip("#sprite", false)
	end
	if action_id == hash("MOVE_LEFT") then
		self.speed.x = self.runSpeed * -1
		sprite.set_hflip("#sprite", true)
	end
end

Oh my bad. :sweat_smile:

Well now all of the sudden every time I try to test my game it says:
image

No idea why. What if you do Project->Build (CTRL/CMD+B)?

now it says
image

Have you tried anything of your own to solve this? Have you restarted Defold? Have you tried a different project? Check your task manager if there’s a dmengine.exe still running, and if so shut it down. Have you restarted your computer?

1 Like

yes, yes, no, yes, yes. I had to restart multiple times and now it is fixed.

Now my dynamic physics object is going flying. I have a static object on at the bottom (not near the dynamic one). I have tried changing the mass and all the physics settings in the object and game.project.

No idea really. I suggest that you strip your project down to a bare minimum first and compare to what you have now. A dynamic collision object will interact with the static one and bounce around. How much depends on gravity (in game.project) and restitution (on collision objects). Here’s a small example of two dynamic objects colliding with some static objects and each other:

1 Like