如何用单键进行全跳

how to make full jump with single press

这是一个非常简单的问题,但即使在谷歌搜索之后我也没有得到正确的想法。

我有一个我想跳的英雄。我已经按照我的逻辑编写了代码,但问题是我必须按住键才能执行整个跳跃。可以说它会走 y-= 20,但如果我按住按钮,它就会走完整个距离。但是,如果我点击一次它不会走完/超过整个距离。

我想让我的英雄按一下就跳完全程,我的意思是只按 1 次而不是按住按钮。

这是我的代码

import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;

var grav:int = 0;
var floor = 450;
var jumping:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , update);

function gravity ():void
    {
        hero.y += grav;
        if (hero.y+hero.height/2 <floor){
            grav++;
        }

        else {
            grav = 0;
            hero.y = floor - hero.height/2 ;

            }

    }


function keyDownHandaler(Devent:KeyboardEvent):void
{
        if (Devent.keyCode == Keyboard.W)
            {
                jumping = true;
            }
}

function KeyUpHandaler (Uevent:KeyboardEvent):void 
{
    if (Uevent.keyCode == Keyboard.W)
        {
            jumping = false;
        }
}

function update(Levent:Event):void
{
    if (jumping)
        {
            hero.y -= 20;
        }

gravity();

}

我认为您需要解决的技巧是保持 "jumping" 标志设置,直到整个跳跃序列完成。目前,您会在按钮抬起后立即重置,然后在重新按下时再次设置。 实际上没有理由仅仅因为用户解除了按钮就关闭了跳跃标志。

就在这里:

hero.y += grav;
        if (hero.y+hero.height/2 <floor){
            grav++;
        }

您可以检查一下角色是否落地。跳跃序列应该在那里结束。

你对你的角色所做的所有事情都不应该在 KEY DOWN 上被跟踪。它应该在 jumping=true 上被跟踪。按下的键只是启动序列。如果已经 运行.

,您确实需要保留该标志,以免再次启动序列

添加是因为第一个答案已经很长了。这是要求重写 OP 代码的答案:(最后阅读注释)

import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;

var grav:double = 0.025;  //Change this to make gravity stronger or weaker.
var gravInc:double = .2;  //Changed to "double" because int is too large an increment.  If double doesn't work, try float or decimal.
var floor = 450;
var jumping:Boolean = false;


stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , update);



    function gravity ():void
    {
        if (hero.y+hero.height/2 < floor){
            //grav++;  //not needed
            gravInc = gravInc - grav;  //GravInc starts strong, and gradually decreases.  It will eventully be less than zero, and start negetive.
            hero.y -= gravInc;  //The effect of which is the chracter will move upward until gravity takes over, and make hero move downward - increaseing speed until it hits the floor.
        } else {
            //grav = 0;  Not needed
            hero.y = floor - hero.height/2 ;
            //We've hit the floor.  Stop jump sequence
            jumping = false;
        }
    }


function keyDownHandaler(Devent:KeyboardEvent):void
{
        if (Devent.keyCode == Keyboard.W)
            {
                if (jumping==false) {  //The character is not already jumping
                    //This initiates the jump sequence
                    //set up the jump sequence by resetting the gravInc and setting jumping to true
                    gravInc = .2;
                    jumping = true;

                    //Start the jump now so that the character is off the floor
                    hero.y -= gravInc;
                }
            }
}

function KeyUpHandaler (Uevent:KeyboardEvent):void 
{
    if (Uevent.keyCode == Keyboard.W)
        {
//            jumping = false;
//            Not needed here
        }
}

function update(Levent:Event):void
{
  //  This whole thing goes.  It is not needed because the jumping==true will handle it.
  //  if (jumping)
  //      {
  //          hero.y -= 20;
  //      }

    if ( jumping==true ){ 
        gravity(); 
    }

}

我无法测试此代码。我注释掉了代码中不必要的部分,并添加了使其正常运行所需的内容。

ActionScript 语法与 JavaScript 非常相似。所以我把这个 link 作为一种演示来展示整个重力在实际程序中是如何工作的。 Bounding balls

查看该页面上的源代码。

希望这对你有用。