AS3平滑跳跃 [英] AS3 Smooth Jumping

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

问题描述

我想知道如何让我的比赛中顺利跳跃。它是一种2D游戏和code是非常简单的,但我想知道如何使它更好地为它慢下来当它到达最大高度,然后平稳下降。

这是我的全部跳跃:

  Player.y  -  = 50;
 

解决方案

您最好的选择是使用物理引擎(Box2D的等)。如果你不想要一个,虽然开销(如果你使用它的唯一的事情就是跳不冲突),那么你只需要一些摩擦添加到您的逻辑。

  VAR摩擦:数= 0.85; //如何快速减速/加速 - 数字越低,就越快(必须小于1,和大于0至正常工作)
变种速度:数= 50; //多少移动每个增量,重置每跳为默认值
VAR方向:= -1; //重新设置为-1,每次跳开始

功能jumpLoop(){//让我们假设这运行的每一帧一边跳
    player.y + =速度*方向; //取当前速度,并把它应用在当前方向
    如果(方向℃,){
        速度* =摩擦; //降低速度的球员上升
    }其他{
        速度* = 1 +(1  - 摩擦); //增长速度,现在球员正在下降
    }

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

I would like to know how to make a smooth jump in my game. Its a 2D game and the code is really simple but I would want to know how to make it better for it to slow down when it gets to the max height and then smooth drop.

This is all I have for jumping:

Player.y -= 50;

解决方案

Your best bet would be to use a physics engine (Box2d etc). If you don't want the overhead of one though (if the only thing you'd use it for is jumping and not collisions) then you just need to add some friction to your logic.

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 = 50;  //how much to move every increment, reset every jump to default value
var direction   :int = -1;  //reset this to -1 every time the jump starts

function jumpLoop(){ //lets assume this is running every frame while jumping 
    player.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(player.y > stage.stageHeight - player.height){  //stage.stageheight being wherever your floor is
        player.y = stage.stageHeight - player.height; //put player on the floor exactly
        //jump is over, stop the jumpLoop
    }
}

这篇关于AS3平滑跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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