vendredi 25 septembre 2015

AS3 landing smoothly under affect of Gravity

I'm attempting a to make a Fighting game with multiple platforms. I've currently successfully made the controls, movement, (double)jumping, and gravity parts of the game.

My problem is, when a player jumps (more visible when the player Double jumps), when they reach the ground, they seem to go a bit deeper than they should on the platform (they should land and stay on the surface of the platform).

I know why this happens; it's because sometimes hitTestObject takes a while to react when objects come in too quickly.

So, the first thing I thought up is to make the player's foot's y axis equal to the y axis of the top of the platform he lands on.

The problem is; that solution resulted in a rather jerky landing.

So, my question is; Is there a way to make the player to land smoothly on the top surface of the platform?

Some things I've tried:

-Raising FPS, it just made the same effect happen, but more quickly.

-Decreasing the speed at which the player falls, but that makes the game less fun, so I'm crossing it out.

And here's my Code:

Gravity and Jumping:

stage.addEventListener(Event.ENTER_FRAME, loop);
var jumpConstant:Number = 30;
var gravityConstant:Number = 1.8;
function loop(e:Event):void //happens 
{
    if(player.leg1.foreleg1.foot1.hitTestObject(platforms.ground)||player.leg2.foreleg2.foot2.hitTestObject(platforms.ground)) //if either of the player's legs are colliding with the platform [ps: nested movieclips]
    {
        player.ySpeed = 0; //the player stops going downwards
        player.y = platforms.y; //the player's y becomes the platform's y. don't worry, it puts the player in the right place, just not smoothly.
        if (player.b_up) //if Up control button (W or Up Arrow) is being pressed
        {
            player.ySpeed = -player.jumpConstant; //make the player jump

        }
    else //if the player isn't colliding with the platform
    {
        player.ySpeed += player.gravityConstant; //the player is affected by gravity and is pulled downwards
    }

It has more code than that, but that's all that's relevant.

Link to the game if you wanna try it out to see the jerky effect:

http://ift.tt/1KzXZRX

Thank you in advance :)

Aucun commentaire:

Enregistrer un commentaire