ActionScript 3, How to get character to jump for longer

两盒软妹~` 提交于 2019-12-25 07:06:20

问题


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:


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.



来源:https://stackoverflow.com/questions/32239461/actionscript-3-how-to-get-character-to-jump-for-longer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!