ActionScript 3, How to get character to jump for l

2019-08-16 11:18发布

I am making a platformer game in Flash (AS3) and the code I have below works. I want my character to jump high enough to allow it time to reach a platform. The only problem with code below is the speed at which it jumps up and down and the height of the jump. Tthe space bar is what triggers the function to run.

Please help as I would much appreciate it! :)

Player.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
    if (spacePressed){
        var gravity:Number = 9.8;
        var jumping:Boolean = false;
        var jumpVar:Number = 0;

        if(jumping != true)
        {
            jumpVar = -70;
            jumping = true;
        }

        if(jumping)
        {
            spacePressed = false;
            Player.y += jumpVar;
            jumpVar += gravity;
        }
        Player.addEventListener(Event.ENTER_FRAME, drop);
        function drop(event:Event)
        {
            Player.y -= jumpVar;
            jumpVar -= gravity;
            if(Player.y > 350){
                Player.y = 350;
            }
        }
        Player.removeEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
        Player.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

        /*var frameNumb:Number = 0;

        Player.addEventListener(Event.ENTER_FRAME, jumpup);
        spacePressed = false;
        function jumpup(event:Event)
        {
            while(frameNumb < 30){
                spacePressed = false;
                Player.y -= 1;
                frameNumb += 0.5;
            }
            Player.removeEventListener(Event.ENTER_FRAME, jumpup);
            Player.addEventListener(Event.ENTER_FRAME, jumpdown);
            function jumpdown(){
                while(frameNumb > 0){
                    spacePressed = false;
                    Player.y += 1;
                    frameNumb -= 0.5;
                }
            }
        }*/
    }
    if (leftPressed)
    {
        Player.x -= speed;
        Player.gotoAndStop("left");
    }
    if (rightPressed)
    {
        Player.x += speed;
        Player.gotoAndStop("right");
    }
}

Thanks

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-16 11:44

Your use of 9.8 for gravity is meters-per-second-per-second. Since drop() is executed every frame, you're going to get some serious fast-forward gravity, unless the program is doing only 1 FPS. So, assuming you want more fluidity than 1 FPS, consider doing

    jumpVar += gravity/fps;


However, to get the exact velocity required to lift you to a height, I think the calculation is...

    initialVelocityInMetersPerSecond = Math.sqrt( 2 * gravity * metersToJump )

So instead of jumpVar = -70, you would do something like...

    // get posititive distance since we'll use to get a square root
    var metersToJump:Number = Player.y - platform.y;
    jumpVar = -Math.sqrt( 2 * gravity * metersToJump );

...and then in the ENTER_FRAME handler...

    Player.y += jumpVar / fps;
    jumpVar += gravity / fps;

From your example it's not the case, but if you place the platform below the Player, it won't work as you can't get the root of a negative number!

In my example code I am not fixing the height of the platform, so how you decide on the target platform is a completely separate matter.

查看更多
登录 后发表回答