繁体   English   中英

AS3角色跳跃/掉落问题

[英]AS3 Character jumping/falling problems

按下向上箭头时,我无法使角色跳转/失败。 我正在尝试做到这一点,因此一旦按下向上箭头键,它将跳到一定高度然后掉落。

这是我的移动代码:

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()
        }

您的问题是您需要随着时间的推移增加玩家的位置(而不是一次全部)。

您可以使用补间引擎(例如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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM