因此,玩家可以使用空格键跳跃,并将重力拉向地板。我想要的是,如果他不再在场上,你就不能再次跳跃,直到你摔倒。我尝试制作一个布尔值并实现它的运动,但仍然没有结果。知道怎么做吗?
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;
}
}
答案 0 :(得分: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轴上的玩家位置。
如果继续这样做,您甚至可以检查帧之间的时间并将速度乘以时间以获得平滑的结果。
祝你好运。