AS3 Juming / Gravity问题

时间:2013-01-16 19:25:23

标签: actionscript-3 movieclip gravity

我遇到的问题是我不知道如何让角色停止跳跃。

例如,当我经常撞到太空时,它会一直跳跃和跳跃。当我按空格键时我希望它跳跃而MC跳跃时我想禁用空格键(或者如果可能的话,你也可以告诉我如何禁用MouseEvents)这样在空中时MC只能跳一次。

var gravity = 0.8;
var floor = 251;
player.y = floor;
player.speedY = 0;
player.impulsion = 10;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
    player.speedY += gravity;
    player.y += player.speedY;
    if(player.y > floor) {
        player.speedY = 0;
        player.y = floor

    }

}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
    if(e.keyCode == Keyboard.SPACE) {
        player.speedY = -player.impulsion
    }
}

1 个答案:

答案 0 :(得分:1)

我通常只有一个像“onFloor”这样的布尔变量,当图层落地时设置为true,跳转时设置为false。然后只有当它们是onFloor时才让它们跳跃。所以我在if(player.y > floor)内将其设置为true,if(e.keyCode == Keyboard.SPACE && onFloor)后为false:

var gravity = 0.8;
var floor = 251;
var onFloor:Boolean = false;

player.y = floor;
player.speedY = 0;
player.impulsion = 10;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
    player.speedY += gravity;
    player.y += player.speedY;
    if(player.y > floor) {
        player.speedY = 0;
        player.y = floor;
        onFloor = true;
    }

}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
    if(e.keyCode == Keyboard.SPACE && onFloor) {
        player.speedY = -player.impulsion;
        onFloor = false;
    }
}