ActionScript 3的,如何让人物跳跃更长 [英] ActionScript 3, How to get character to jump for longer

查看:114
本文介绍了ActionScript 3的,如何让人物跳跃更长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个成熟的游戏在Flash(AS3)和code我下面的作品。我想我的性格跳的高,足以让它的时间到达一个平台。低于code唯一的问题是,在它跳起来的速度和向下跳的高度。里边反空格键是什么触发功能来运行。

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.

请帮忙,我会多AP preciate吧! :)

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");
    }
}

感谢

推荐答案

您使用9.8重力是米每秒每秒。由于降()被执行每一帧,你会得到一些严肃的快进重力,除非该程序正在做只有1 FPS。因此,假设你想比1 FPS较高的流动性,可考虑做

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 )

所以不是jumpVar = -70,你会做这样的事情......

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 );

...然后在ENTER_FRAME处理...

...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!

在我的例子code我没有固定平台的高度,所以你如何决定在目标平台上是一个完全独立的问题。

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.

这篇关于ActionScript 3的,如何让人物跳跃更长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆