AS3角色跳跃/坠落问题

时间:2014-11-19 17:55:37

标签: actionscript-3 actionscript game-physics

按下向上箭头时,我遇到跳跃/失败的问题。当我按下向上箭头键时,我会尝试这样做,一旦它会跳到一定高度然后再下降。

这是我的动作代码:

public var gravity:int = 2;



public function fireboyMovement(e:KeyboardEvent):void
    {
        if (e.keyCode == 37) //right
        {
            fireboy1.x = fireboy1.x -5;
            checkBorder()

        }
        else if (e.keyCode == 39) //left
        {
            fireboy1.x = fireboy1.x +5;
            checkBorder()

        }
        if (e.keyCode == 38) //up
        {
            fireboy1.y = fireboy1.y -20;
            fireboy1.y += gravity;
            checkBorder()
        }

1 个答案:

答案 0 :(得分:1)

你的问题是你需要随着时间的推移增加球员的位置(而不是一次性增加)。

您可以使用补间引擎(如tweenlite),也可以滚动自己的计时器或输入帧处理程序。

以下是后者的一个例子:

    if (e.keyCode == 38) //up
    {
        if(!isJumping){
            isJumping = true;
            velocity = 50;  //how much to move every increment, reset every jump to default value
            direction = -1; //start by going upwards
            this.addEventListener(Event.ENTER_FRAME,jumpLoop);
        }
    }

var isJumping:Boolean = false;
var friction:Number = .85; //how fast to slow down / speed up - the lower the number the quicker (must be less than 1, and more than 0 to work properly)
var velocity:Number;
var direction:int = -1;

function jumpLoop(){ //this is running every frame while jumping 
    fireboy1.y += velocity * direction; //take the current velocity, and apply it in the current direction
    if(direction < 0){
        velocity *= friction; //reduce velocity as player ascends
    }else{
        velocity *= 1 + (1 - friction); //increase velocity now that player is falling
    }

    if(velocity < 1) direction = 1; //if player is moving less than 1 pixel now, change direction
    if(fireboy1.y > stage.stageHeight - fireboy1.height){  //stage.stageheight being wherever your floor is
        fireboy1.y = stage.stageHeight - fireboy1.height; //put player on the floor exactly
        //jump is over, stop the jumpLoop
        this.removeEventListener(Event.ENTER_FRAME,jumpLoop);
        isJumping = false;
    }
}