0

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

例如,当我不断地击中空间时,它会不停地跳跃。当我按下空格键时,我希望它跳跃,而当 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
    }
}
4

1 回答 1

2

我通常只有一个像“ onFloor”这样的布尔变量,当图层着陆时设置为真,当他们跳跃时设置为假。然后只有在他们跳的时候才让他们跳onFloor。所以在这里我在你的内部将它设置为 true if(player.y > floor),在之后设置为 false if(e.keyCode == Keyboard.SPACE && onFloor)

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;
    }
}
于 2013-01-16T19:42:26.930 回答