如何用单按完全跳跃

时间:2016-05-09 16:28:37

标签: actionscript-3 flash

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

我有一个英雄,我想跳。我已经在我的逻辑中编写了代码,但问题是我必须按住键来执行整个跳转。让我们说它会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();

}

2 个答案:

答案 0 :(得分:3)

我认为你需要解决的诀窍是保持&#34;跳跃&#34;标志设置,直到整个跳转序列完成。目前,只要按钮抬起,它就会重置,然后在重新按下时再次设置。 实际上没有理由因为用户抬起按钮而关闭跳跃标志。

就在这里:

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

你要检查一下这个角色是否已经发现了。它应该在那里跳转序列完成。

不应在KEY为DOWN的情况下跟踪你对角色所做的所有事情。应该在jump = true上跟踪它。按下的键只是启动序列。而且你确实需要保留该标志,以便在它已经运行时不再启动它。

答案 1 :(得分:0)

已添加,因为第一个答案已经很长了。这是请求重写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非常相似。因此,我将此链接作为一种演示来展示整个引力在实际程序中的工作原理。 Bounding balls

查看该页面上的来源。

希望这对您有用。