AS3性格跳跃​​/下降的问题 [英] AS3 Character jumping/falling problems

查看:126
本文介绍了AS3性格跳跃​​/下降的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,让人物跳跃/失败时,向上箭头为pressed。我想使它所以当向上箭头键pressed一旦它会跳到一定高度后下降。

下面是我的运动code:

 公共变种比重:INT = 2;



公共职能fireboyMo​​vement(E:的KeyboardEvent):无效
    {
        如果(e.key code == 37)//权
        {
            fireboy1.x = fireboy1.x -5;
            checkBorder()

        }
        否则,如果(e.key code == 39)//离开
        {
            fireboy1.x = fireboy1.x +5;
            checkBorder()

        }
        如果(e.key code == 38)//起来
        {
            fireboy1.y = fireboy1.y -20;
            fireboy1.y + =重力;
            checkBorder()
        }
 

解决方案

您的问题是,你需要增加球员的位置随时间(而不是一次)。

您可以使用一个补间动画引擎(如TweenLite的),或推出自己的定时器或输入帧处理。

下面是后者的一个示例:

 如果(e.key code == 38)//起来
    {
        如果(!isJumping){
            isJumping = TRUE;
            速度= 50; //多少移动每个增量,重置每跳为默认值
            方向= -1; //通过去向上启动
            this.addEventListener(Event.ENTER_FRAME,jumpLoop);
        }
    }
 


  VAR isJumping:布尔=假;
VAR摩擦:数= 0.85; //如何快速减速/加速 - 数字越低,就越快(必须小于1,和大于0至正常工作)
变种速度:数;
VAR方向:= -1;

功能jumpLoop(){//这是每一个运行的框架,同时跳
    fireboy1.y + =速度*方向; //取当前速度,并把它应用在当前方向
    如果(方向℃,){
        速度* =摩擦; //降低速度的球员上升
    }其他{
        速度* = 1 +(1  - 摩擦); //增长速度,现在球员正在下降
    }

    如果(速度&小于1)方向= 1; //如果玩家现在正小于1个像素,改变方向
    如果(fireboy1.y> stage.stageHeight  -  fireboy1.height){//stage.stageheight是无论你的地板
        fireboy1.y = stage.stageHeight  -  fireboy1.height; //把球员在场上完全
        //跳结束后,停止jumpLoop
        this.removeEventListener(Event.ENTER_FRAME,jumpLoop);
        isJumping = FALSE;
    }
}
 

I am having problems with getting the character to jump/fail when the up arrow is pressed. I'm trying to make it so when the up arrow key is pressed once it will jump a certain height then fall.

Here is my movement code:

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

解决方案

Your issue is you need to increment the players position over time (and not all at once).

You can either use a tweening engine (like tweenlite), or roll your own timer or enter frame handler.

Here is an example of the latter:

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

这篇关于AS3性格跳跃​​/下降的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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