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.
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.
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
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")
Ah… I’m . 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.
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
I tried debugging with your code sample above. Great suggestion!
It is clear that everything goes through, because I get the confirmation when I’m standing on ground, running, jumping etc!
It seems like everything is communicating and firing properly so at this point I have absolutley no idea why the animations aren’t playing.
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.
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:
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