AS3 Плавные прыжки

Я хотел бы знать, как сделать плавный прыжок в моей игре. Это 2D-игра, и код действительно прост, но я хотел бы знать, как сделать так, чтобы она замедлялась, когда она достигает максимальной высоты, а затем плавно падает.

Это все, что у меня есть для прыжков:

Player.y -= 50;

person deathwillcome800    schedule 11.08.2014    source источник


Ответы (2)


Лучше всего использовать физический движок (Box2d и т. д.). Однако, если вы не хотите накладных расходов (если единственное, для чего вы будете его использовать, это прыжки, а не столкновения), вам просто нужно добавить некоторое трение в свою логику.

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
    }
}
person BadFeelingAboutThis    schedule 11.08.2014

Скопируйте/вставьте следующий код... jump() можно заменить на jump2() (без эффекта подпрыгивания). Прыжки будут производиться пробелом:

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;
        }
}
*/
person helloflash    schedule 11.08.2014