AS3 Smooth Jumping

一曲冷凌霜 提交于 2019-12-13 06:56:31

问题


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;

回答1:


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



回答2:


Copy/paste the following code... jump() can be replaced by jump2() (without bouncing effect). The jumping will be produced by the space bar:

const FA:Number = .99; // air resistance
const CR_BM:Number = .8; // bouncing coefficient
const µ:Number = .03; // floor friction
const LB:int = stage.stageHeight; // floor (bottom limit)
const G:int = 2.5; // gravity
const R:int = 50;

var ball:MovieClip = new MovieClip();
this.addChild(ball);
var ba:* = ball.graphics;

ba.beginFill(0xFFCC00);
ba.lineStyle(0, 0x666666);
ba.drawCircle(0, 0, R);
ba.endFill();

ball.vx = 2;
ball.vy = -30;
ball.r = R;
ball.x = 100;
ball.y = LB - R;

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(e:KeyboardEvent):void {
    if (e.keyCode == Keyboard.SPACE) {
        ball.vy = -30;
        addEventListener(Event.ENTER_FRAME, jump);
    }
}

function jump(e:Event):void {
        ball.vy = ball.vy + G;
        ball.vx *= FA;
        ball.vy *= FA;
        ball.x += ball.vx;
        ball.y += ball.vy;
        if (ball.y > LB - ball.r) {
            ball.y = LB - ball.r;
            ball.vy = -1 * ball.vy * CR_BM;
            ball.vx += ball.vx * - µ;
        }
}

/*
function jump2(e:Event):void {
        ball.vy = ball.vy + G;
        ball.vx *= FA;
        ball.vy *= FA;
        ball.x += ball.vx;
        ball.y += ball.vy;
        if (ball.y > LB - ball.r) {
            ball.y = LB - ball.r;
        }
}
*/


来源:https://stackoverflow.com/questions/25246962/as3-smooth-jumping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!