Animation Trouble


  • Posts: 65

Here's a screenshot of what I have so far and I'm very proud of it considering that it's my first time to use this will little to none knowledge on programming.

1) I'm having trouble coding the jump (maybe because the jump animation is more complicated than regular ones

Animation Sequence:
Starting (1 frame on the ground, .2 secs)
Rising (1 looping frame in air until it hits the peak of its arc/jump)
Peak (1 frame at the peak of the arc/jump, .2 secs)
Falling (1 looping frame in air until it hits the ground)
Landing (1 frame on the ground, .2 secs)

How do I get this to work? I'm also having trouble with the facing right facing left situation.. Here are my current events for it:

Running (made this myself very proud of it)


Facing Directions


Sorry if the logic is flawed. It's my first time to use this and it's my first day. Graphics by me btw, my current jump does not play the falling animation when he falls out of gravity (walking off a platform)

2) How do I make it that the character stops in place when both left and right buttons are down at simultaneously (not needed to be at exactly the same time but you get it)

« Last Edit: June 01, 2012, 06:11:24 am by Medevenx »


  • *
  • Posts: 6108
That's some nice graphics you have there ;)

An idea for the peak/falling animations would be to check if the y-speed is smaller or greater than zero. Something like "if jumping and y-speed > 0, switch to peak animation, do after 0.2 seconds switch to falling animations".

To make the falling animation work even when you don't jump, you will need to check if the actor is on ground (similar to how you do in the landing code). Then "if not on ground and y-speed > 0, switch to falling animation".

To stop the actor when left and right keys are pressed:
<if <<Left is down> and <right is down>>>
  <set x-speed for Self to 0>
<otherwise if <Left is down>
 ** enter left movement code here **
<otherwise if <Right is down>
 ** enter right movement code here **

And just a small tip, when working with booleans, instead of
<if <<Boolean> = <true>>
you can just write
<if <Boolean>>

and instead of
<if <<Boolean> = <false>>
you can use
<if <not <Boolean>>>


  • Posts: 65
I've tried that Left is down and Right is down before but it doesn't work

(it picks up the most recent button that has been pressed)

also, if y-speed > 0 and i switch it to peak animation it will replace the jumping animation because it the y-speed when going up is > 0, my choice was to make it y-speed = 0 because the velocity of an object is 0 at its peak but the animation does not occur for some reason

i only need my startup/peak/landing animations to work

also i did that object not on ground and speed > 0 thing but it doesn't work

here's my current code


  • Posts: 184
I'm no stranger to coding, but I'm new to making games xD  So take my advice for what it's worth.

If you have some animation where he's neither facing left or right, then disregard this paragraph xD  Otherwise, first thing's first, you don't need a boolean for facing left and a boolean for facing right.  If the boolean for facing left is false, it implies that the character is facing right.

1.)  IMO, this problem is probably harder than it needs to be, because we're human and a computer is a silly stupid piece of junk.  Theoretically, an object's speed at the peak of a jump is indeed 0.  However, that's in real life.  In virtual reality, "physics" is updated and looked at in certain time intervals.

For example, imagine that a ball is thrown up in the air and is accelerating downwards due to gravity by 10.  If the ball was thrown upwards at an initial speed of 25, and we check the ball's speed every second, we'll see that the ball's speed is 25 at 0 seconds, 15 at 1 second, 5 at 2 seconds, -5 at 3 seconds....  We never find that the ball's speed is 0 from our checks, even though it has to have passed 0 between 2 and 3 seconds.  I don't know how comprehensive the Box2D physics engine is, so this example might not apply.

Another problem is that the animation may happen so fast, that we just don't see it.  I've noticed that in your code in snippetcode.png, your intent is that the peak animation should happen for 0.2 seconds.  That's good.

However, there's one more problem that I'm spotting specifically in your code.  Assuming that the computer does find that your character's y-speed is 0 and that he's not on the ground, your peak code is being overriden by another code.  Since they're both "if" blocks, they're given equal consideration by the computer.  Even though you specified that the character should show the falling animation after 0.2 seconds in the peak animation code, before that 0.2 seconds has elapsed, you already meet the requirements for the falling animation code: character is not on the ground, and character's y-speed is greater than 0.  The computer will follow this instruction, even though it hasn't been 0.2 seconds yet.  The result is that you get the falling animation MUCH sooner than you intended.

2.)  The order that you tell the computer to check things matters.

In snippetcode4.png, the case that captaincomic suggested is in an "otherwise if" block.  Since the first "if" block is for holding down the right key, the computer will follow the code for that block and disregard all other blocks that follow that say "otherwise if" and "otherwise".  In other words, you'll never ever reach the code that causes the character's speed to be 0 if left and right are pressed together.

In your current code, if you jam down both the left and right keys, your character will run right.  That's because the code for running right comes first.

Generally what you want to do is to tell the computer to look at the most specific cases first, so they have a chance of actually running.  Your second problem can easily be solved at this point by just moving the code for left+right key pressed in the first "if" block, and have everything else follow in "otherwise if".

« Last Edit: May 31, 2012, 11:59:48 pm by Kajitii »


  • *
  • Posts: 6108
Negative y-speed is moving up, positive y-speed is moving down. That's why I would check if y-speed > 0 for the peak/falling animation. Checking for y-speed = 0 won't work for the reasons given by Kajitii.


  • Posts: 65
Alright the running has been fixed thanks to that. I'm still learning about which statements such as if and otherwise if and others are taken to the highest priority.

Anyway, I'm having a problem with the simplest of things. The start jump/land animation is actually just 1 frame. But it won't even do that when I press jump! It's been set in the actor's behaviour already and should be played for exactly .2 seconds. I wish for the peak animation which is ALSO a single frame which i actually plan to have him stay in the air for that .1 second (not descend but return to his descending mode only after it)

I just remembered i wanted variable jumping where you know, the less you hold down the jump button the less and there is a limit etc.. how do i do that know that everything is so complicated? i still haven't gotten the landing to work either


  • Posts: 184
Again, an issue you're running into is the timing and conditions that the computer will execute the code.

In the new code snippet you posted, the actor doesn't even get off of the ground.  And in the old code snippet (snippetcode.png), you have an "if" block with the same conditions.  Why not just put both of them in the same block?

Since the old block that initiates the jumping sequence simply sets the "On Ground?" boolean to false, you have to consider what will happen after that.  Turns out you have an "if" block that, when the "On Ground?" boolean becomes false, will immediately show the ascending jumping animation.  Well, okay maybe not instantly, since it does take 0.075 seconds to set the boolean to false when the player hits the jump key.  For aesthetic reasons mostly, I'm thinking of setting this time to 0.2 seconds and have the player show the start jump/land animation while the clock is running down.  This will make the controls a bit wonky though =/

For the rest of the sequence, here's what I'm thinking that should work: show the ascend animation when y-speed < -15, set the peak animation to show when y-speed is close to zero (absolute value of y-speed < 5), and show the descend animation when y-speed > 15.  Tweak the numbers as you see fit, so that the animation looks smooth.  I doubt that the numbers I used are good xD.  This should cover a lot of cases, such as what happens if your character hits his head on the ceiling, or what happens if your character runs (not jumps) off of a cliff.  Or hell, ends up levitating or going places via wind tunnels, oscillating gravity fields, etc.  Maybe someone can come up with something better than I can, but this seems to be reasonable to me.

If you're absolutely adamant about having the animations show for 0.2 seconds, regardless of what happens, I would advise having extra checks in the form of booleans, so that other code doesn't accidentally fire off.  For example, "Reach Peak?" will become true after the ascend portion of the jump is finished, and the descend portion of the jump will not show its animation until "Reach Peak?" becomes false 0.2 seconds later.

And when landing, just get the animation to play when the boolean "On Ground?" is set to true.  If that's all you did though, then it will only play when the player doesn't press any keys when the character lands.  You'll probably need another boolean to ensure that the landing animation plays for a set period of time, regardless of what the player does upon landing (including jumping again).

As for variable jumping... ooh boy that's a whole new challenge in itself!  One step at a time please!  There's many ways to do this, but each of them generate a different jumping arc or "physics".  I believe the control condition that says something like "when jump was released" will be vital to what you want, but at the same time make sure that the player can't jump higher than you intend him to.

« Last Edit: June 01, 2012, 10:58:55 am by Kajitii »


  • Posts: 65
i know he doesn't get off the ground in that snippet. I'm wondering right now why doesn't the animation play at all

here's my newest code that doesn't play ANY of the jumping animations

EDIT: also, when i press him against a wall while in the air he just stops and gets glued to the wall unless i release the button

« Last Edit: June 01, 2012, 01:38:47 pm by Medevenx »


  • Posts: 184
The big reason (and it might not be the only one) that your jump animations aren't working is because you never set "On Ground?" to false.

As for the wall-sticking problem, I couldn't tell you with the information I have.  You need to be more specific in your details.  Are you sticking on the wall while ascending, or descending?  Are your tile collisions the correct shape?  Which button are you talking about?

Again, tackle your problems one step at a time.  If you introduce a new bit of code and then test, and something breaks, you know it is related to that new bit of code you did.  If you introduce a bunch of new code, test, and something breaks, you have a lot more to go through to figure out the problem.  Not fun.  I personally would remove the stuff related to variable jumping until you fix your current problems.


  • Posts: 65
I set that On Ground? to false when created I just didn't post it because it was like 1 line of code

EDIT: do you want to try fixing it? tell me i'll send what i have so far

« Last Edit: June 01, 2012, 08:53:37 pm by Medevenx »


  • Posts: 184
I could take a look at it, though I'll be pretty busy over the weekend.  Doing things with relatives, 2x IP weekend in LoL will be grabbing my attention.

If you want me to fix it and all that, PM me links to your assets and code.  I'll try to make my best effort to tell you how to do things properly.  But in the long-run, it's better to either learn the coding or have a dedicated programmer working for you.


  • Posts: 65
Thanks! I still want to learn really but I just don't understand what I'm doing wrong so thanks for helping me out :)


  • Posts: 184
Okay, so after I did my day's worth of partying and playing with friends, I took a look at your stuff, and made changes:
-- Peak animation is set to show when y-speed is close to 0.
-- Start/end animations are set to show when the actor jumps and when actor has fallen a set distance.  Running movement is disabled during these wind-up/recovery times.

What I didn't do (yet):
-- Variable jumping.  While not terribly difficult to implement, it would take much more time to thoroughly test to make sure that it is working correctly.
-- Fix the wall-sticking glitch.  I'm fairly convinced that collision detection on the actor itself is to blame.  It is also the source of "gimped" and "super" jumps that I've experienced when screwing around with the jump start animation timings.

I figured I would post this for anyone else that has had problems similar to the OP.

Code02.png is the jumping behavior.
Code03.png is the running behavior.


  • Posts: 65
Thank you for that! You deserve an award! Or a game. lol I'll have to try it when I get home from university. Is there a way to upload it directly to Stencyl? (Because I assume it won't)

Sorry about the inconsistent box sizes I didn't do it properly yet because I haven't uploaded all his animations yet..

The question is does his hitboxes have to be identical for every animation? I mean, what if he's ducking? Or do I only have to be consistent with my x values for the hitboxes?


  • Posts: 184
The actor's hit boxes do not have to be identical, though it largely depends on what he's intended to be doing.  For example, all animations where the actor is on the ground should have a consistent bottom collision bound with respect to their top-left corner (or "origin").

Ducking is definitely a case where the hit box should be different =P