因此,玩家可以使用空格键跳跃,并有重力将他拉向地板。我想要的是,如果他不在地板上,你不能再按跳跃,直到你跌倒。我尝试制作一个布尔值并将其实现为运动,但仍然没有结果。知道我该怎么做吗?
var keyPressed:int = -1;
player.addEventListener(Event.ENTER_FRAME, iMoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, iSetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, iUnsetKeyPressed);
function iSetKeyPressed(event:KeyboardEvent):void
{
if (canJump == true) {
keyPressed = event.keyCode;
} else if (canJump == false) {
keyPressed = -1;
}
}
function iUnsetKeyPressed(event:KeyboardEvent):void
{
keyPressed = -1;
}
function iMoveInDirectionOfKey(event:Event)
{
player.y += keyPressed == Keyboard.SPACE ? -20 : 0;
}
var gravity:Number = 15;
var floor:Number = stage.stageHeight - player.height / 2 + 35;
var canJump:Boolean = true;
iCanJump(canJump);
player.y = floor;
player.addEventListener(Event.ENTER_FRAME, iGravity);
function iGravity(pEvent)
{
if (keyPressed != Keyboard.SPACE) {
player.y += gravity;
}
if (player.y > floor) {
player.y = floor;
}
}
function iCanJump (canJump:Boolean) {
if (player.y == floor) {
canJump = true;
}
if (player.y < floor) {
canJump = false;
}
}