Animations not changing from default (SOLVED)

Hi, newbie here!

I’m making a small 2D game and currently the biggest issue I face is not being able to animate the game character. I thought it would be easy since I’m using the code from the tutorials. Basically, I copied the code from the “platform” tutorial and I’m using the same inputs for moving and have 4 animations made called “run”, “idle”, “jump” and “fall”, but with other image files.

The only difference I have made that is since my game is meant to be retro i use low-fps animations and have some of them set to 1 and 2 fps (jump and idle), and I have also changed the values of the gravity and jump speed to get a faster jump.

I really don’t know why the animation doesn’t change from the default one when moving or jumping even tho I’ve read the code (the best I’ve could) and I’m currently trying to understand what I didn’t do.

//Appl3

Have you set the Playback for the animations to Loop Forward in your atlas file? Are you still receiving the input (ie, is the character moving and jumping)? Could you please post the snippet of code that plays the animation and perhaps also a screenshot of the atlas with your animations?

Yeah, I have set the “run” and “idle” animations to loop forward and the “jump” and “fall” ones to play once forward. The character can move and jump just fine, no problems there, which makes me believe there isn’t anything wrong with the code controlling the inputs.

Code controlling the animations:

local function play_animation(self, anim)

--Only play animations that are not already playing
if self.anim ~= anim then

--Msg the sprite to play the animation
	msg.post("#sprite", "play_anim", {id = anim})
	
--Remember which animation is playing
	self.anim = anim
    end
end


local function update_animations(self)
--Make sure the character is facing the correct way
sprite.set_hflip("#sprite", self.direction < 0)

--Make sure the right animation is being played
if self.ground_contact then
	if self.velocity.x == 0 then
		play_animation(self, anim_idle)
	else
		play_animation(self, anim_run)
	end
else
	if self.velocity.y > 0 then
		play_animation(self, anim_jump)
	else
		play_animation(self, anim_fall)
	end
end
end

This is it (I think). Again, I’m using the exact same code that is in the tutorial and since I’m very new to both making games and Lua I have a difficult time understanding some things that might be obvious, sorry about that.

Also, there is some code controlling the animations while jumping, but I didn’t include that right now since it’s depending on the code snippet above to work properly. The only part of it controlling animations is calling the function play_animation.

Here’s a screenshot of the hero.atlas file

Try using;
play_animation(self, hash("run"))

The magic part being hash( ).:slight_smile:

PS: Cool character. :slight_smile:

Like this?:

if self.ground_contact then if self.velocity.x == 0 then play_animation(self, hash("idle")) else play_animation(self, hash("run")) end else if self.velocity.y > 0 then play_animation(self, hash("jump")) else play_animation(self, hash("fall")) end

Not sure if you meant that I should do it here but in either case it didn’t fix it :disappointed:
I should probably mention that I already hashed all id:s in the beginning of the script to improve performance:

local msg_contact_point_response = hash("contact_point_response") local msg_animation_done = hash("animation_done") local group_geometry = hash("geometry") local input_left = hash("left") local input_right = hash("right") local input_jump = hash("jump") local input_touch = hash("touch") local anim_run = hash("run") local anim_idle = hash("idle") local anim_jump = hash("jump") local anim_fall = hash("fall")


Hahah thanks! :smile:

Ah… I’m :confused:. Sorry for not being of any help, I will try to implement your code into my project, to see what happens.

Edit: removed initial response, since self.brainfart. :slight_smile:

Stupid question, but have you tried debuging, to see if the conditional statement fires? By putting pprint()s in there, you will get an indicator if everything fires ok.
Example:

if self.ground_contact then
 pprint("Ground contact true")
 if self.velocity.x == 0 then
  pprint("I'm idle")
  play_animation(self, hash("idle"))
 else
  pprint("I'm running")
  play_animation(self, hash("run"))
 end
else
 pprint("Ground contact false")
 if self.velocity.y > 0 then
  pprint("I'm jumping")
  play_animation(self, hash("jump"))
 else
   pprint("I'm falling")
   play_animation(self, hash("fall"))
 end 
end 

Ps: Sorry about the formatting. :slight_smile:

I tried debugging with your code sample above. Great suggestion! :smiley:
It is clear that everything goes through, because I get the confirmation when I’m standing on ground, running, jumping etc! :thumbsup:

It seems like everything is communicating and firing properly so at this point I have absolutley no idea why the animations aren’t playing.

Any more ideas? :slight_smile:

Did you put a pprint() in local function play_animation(self, anim) also? Inside the conditional if self.anim ~= anim the

What about trying to comment out like this;

local function play_animation(self, anim)

--Only play animations that are not already playing
--if self.anim ~= anim then

--Msg the sprite to play the animation
	msg.post("#sprite", "play_anim", {id = anim})
	
--Remember which animation is playing
	self.anim = anim
    --end
end

That way, nothing should stop the msg.post from firing.

If that doesn’t work, there might be a need for some experts. :slight_smile:

I’ll do that and see what happens :slight_smile:

Edit: I just noticed that when I jump the sprite switched to the right animation during a time of what I assume is the first frame of the second. I tried cranking up the fps on the animation “jump” and “fall” to 60fps thinking it would make it play 60 times more often but it didn’t change anything. I removed the comment markings after noticing this and it did the same thing when I jumped. It seems it has been flashing the whole time, but I only noticed it just now.

Edit 2: Based on this I assume that everything is going through:

Are you sure you’ve set the animations to loop? It must be some simple little thing you’ve overlooked.

If you still can’t make it work I can take a look if you sync your project and invite me via the Defold dashboard (bjorn.ritzl@king.com)

1 Like

Yeah I have the animations set to loop forward (not the ‘jump’ and ‘fall’ ones but it seems irrelevant since the jump animation merely flashes everytime you jump no matter what you have it set on)

I’ll invite you but you must know it’s not very well organized :sweat_smile:

So it really was a stupid little thing that was easy to overlook! You’re doing:

msg.post("#sprite", "play_anim", {id = anim})

But you want to be doing:

msg.post("#sprite", "play_animation", {id = anim})

play_animation is the message you need to post to a sprite

3 Likes

Oh thank you so much! You cannot believe how happy I am it’s finally working, thanks so much for taking your time to help me with this! :smile:

1 Like

No problem! Happy to help!

1 Like