2017-02-03 45 views
0

我正在建造一台2D平台游戏机,现在正在试图让我的玩家在按住空格键时跳得更长。现在玩家可以上下跳动,但是当你按住键时,机器人独角兽会出现类似于这种效果的id。我会如何去做这件事?我没有使用相位器或任何东西,而且大多数教程都是使用它。下面有什么,我有我的更新方法:在JavaScript中延长跳跃精灵?

 var gameEngine = this.game; 

if (gameEngine.keyMap["Space"] && !this.jumping) { //only jump if not already in mid jump 

    this.jumping = true; 
    this.animationCurrent.elapsedTime = 0; 
    this.game.space = false; 
} 

if (this.jumping) { 

    if (this.animationCurrent.isDone()) { 
     this.animationCurrent.elapsedTime = 0; 
     this.jumping = false; 
    } 

    var jumpDistance = this.animationCurrent.elapsedTime/
     this.animationCurrent.totalTime; 

    var totalHeight = 150; 

    if (jumpDistance > 0.5) 
     jumpDistance = 1 - jumpDistance; 
    var height = totalHeight * (-4 * (jumpDistance * jumpDistance - jumpDistance)); 
    this.y = this.ground - height; 
    console.log(this.y); 
} 
+0

你使用onkeypressed?如果是这样,这可能是不可能的设计。你在使用onkeydown吗?如果是这样,这将允许您随着时间节省跳跃高度作为奖励。例如,+150,+ 15,+ 5,+ 1。 –

+0

@TravisJ我使用onkeydown – thatsnifty

回答

1

你基本上需要实现由按住跳跃键继续逐渐减少的力施加到角色的系统。

看起来你现在正在将跳跃高度绑定到动画中?这并不完美,调整东西变成球疼。您需要更多基于物理学的方法。信封伪代码的

基本回:

旧风格的不断跳跃的动作:

const JUMP_STRENGTH = 100; 
const GRAVITY = 10; 

onJumpKeyupPress(){ 
    if(play.canJump) 
    player.transform.up.velocity = JUMP_STRENGTH; 

} 

gameTick(){ 
    if(!player.onGround) 
    { 
     player.up.velocity -= GRAVITY 
     player.y += player.up.velocity; 
    } 
    else{ 
     player.up.velocity = 0; 
    } 
} 

正如你可以看到,这里采用重力拉下玩家角色加班。但是,所有的力在一次施用时用户按下跳跃

更大跳转虽然保持跳跃键:

const JUMP_STRENGTH = 100; 
const GRAVITY = 10; 

var jumpStrength = 0; 

onJumpKeyupPress(){ 
    if(play.canJump) 
    jumpStrength = JUMP_STRENGTH; 
} 

gameTick(){ 
    if(!player.onGround) 
    { 
     if(jumpKeyIsStillPressed){ 
      player.up.velocity += jumpStrength; 
      jumpStrength /= 2; 
     } 
     player.up.velocity -= GRAVITY; 
     player.y += player.up.velocity; 
    } 
    else{ 
     player.up.velocity = 0; 
     jumpStrength = 0; 
    } 
} 

在此示例中,用户获得的100的初始跳跃强度,这减半与每个勾选直到重力更强。

希望我已经清楚了!

+0

这工作得很好!我唯一的问题是它跳下来而不是上升,我将如何解决这个问题? – thatsnifty

+0

我想我想通了,我把player.y + = player.up.velocity改成了player.y - = player.up.velocity – thatsnifty

+0

有没有办法让跳得更快?它会上下超慢,即使我改变重力 – thatsnifty