1

因此,玩家可以使用空格键跳跃,并有重力将他拉向地板。我想要的是,如果他不在地板上,你不能再按跳跃,直到你跌倒。我尝试制作一个布尔值并将其实现为运动,但仍然没有结果。知道我该怎么做吗?

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

1 回答 1

1

使用这样的速度变量:

public var jumpSpeed : Number = -20;
public var ySpeed : Number = 0;
public var gravity : Number = 1;
function iSetKeyPressed(e:KeyboardEvent):void
{
    if(e.keyCode == Keyboard.SPACE && player.y == floor)
        ySpeed = jumpSpeed; 
}

player.addEventListener(Event.ENTER_FRAME, iGravity);

function iGravity(e : Event)
{
    player.y += ySpeed;
    if(player.y < floor)
    {
        ySpeed += gravity;
    }
    else
    {
        ySpeed = 0;
        player.y = floor;
    }
}

您基本上为玩家创建了一个速度变量并在每回合更新它。您也可以通过玩家速度更新玩家在 y 轴上的位置。

如果您继续工作,您甚至可以检查帧之间的时间并将速度乘以时间以获得平滑的结果。

祝你好运。

于 2012-09-25T16:51:40.227 回答