When the game starts it is supposed to stay above the ground, but when the game starts it slowly goes down. But when space is pressed the character goes up then back down but if you jump in the middle it stays above the ground, but if you move to the left it will go back down. The movement code is lines 258-281.
Please click on the Share button and provide the link of your app here.
There is a sort of “gravity well” on line 312 which states that as long as KaiIdle
is not touching platform1
, then KaiIdle.velocityY += 0.4;
I’m not sure which sprite represents the ground, but it doesn’t seem to be part of the platform group, and that check is only for that one specific platform sprite I believe.
Could that be what is causing the issue?
Yes, thank you, that was causing the problem, but there is 2 new problems.
-
When “KaiIdle” touches the ground (the sprite for the ground is “ground”) it will stay in the jumping animation.
-
When KaiIdle is in the air and I move left or right, it will start the running left or right animation in air instead of staying on the jumping animation.
I can fix the second problem but I more worried about the 1st problem. Thank you
For the first problem, can’t you just set his animation to idle whenever his velocityY == 0?
I made it so if KaiIdle is above the ground it will switch to the idle animation but the problem is it will not play the running animations when a or d is pressed.
Looks like you fixed it. a and d key are working for running animation for me:)
yay! ~Michelle
I see, he gets stuck and looks like he’s skating along the ground. Probably because the loop keeps telling it to go back to idle after changing to that first frame.
One thing I do for exceptional events, although it tends to get a little complicated, is create a boolean variable, such as var grounded = true;
You could make it so that whenever Kai jumps, grounded is set to false, and when he hits the ground, it is set back to true. This gives you two different states to play with, so that you can ensure that the jumping animation only plays while he is in the air and you can do whatever other custom things you want to do when he is on the ground (as an added bonus, you could prevent him from displaying the running animation while in the air this way).
With that said, I’m not sure why the running animation doesn’t preempt your jumping animation. Maybe the running controls need to come after the jumping controls in the code so that it reads them last?