2013-12-19 32 views
1

嘿,我在XNA做了一个游戏,游戏是基于玩家跳跃的能力。我遇到很多问题,但最麻烦的是,当我跳跃时,我只能做到这一点,因为它只会发生在hasJumped = false,但我不知道该把它放在哪里。我有一个地板,我试图做一个功能,说如果玩家触摸地板将其设置为假,但我不能得到那个工作。 hasJumped在构造函数中默认为false。跳转只会发生一次

任何人都可以帮助我吗?

this.Transform.MoveIncrement = Vector2.Zero; 
float timeBetweenUpdates = 0.25f * gameTime.ElapsedGameTime.Milliseconds; 

if (game.KeyboardManager.isKeyDown(Keys.Left)) 
{ 
    this.Transform.MoveIncrement += -this.Transform.Look * timeBetweenUpdates; 
    this.Transform.IsMoved = true; 
} 
if (game.KeyboardManager.isKeyDown(Keys.Right)) 
{ 
    this.Transform.MoveIncrement += this.Transform.Look * timeBetweenUpdates; 
    this.Transform.IsMoved = true; 
} 
if (game.KeyboardManager.isKeyDown(Keys.Up) && hasJumped == false) 
{ 
    this.Transform.moveBy(-Vector2.UnitY * 400); 
    this.Transform.IsMoved = true; 
    hasJumped = true; 
} 
if (hasJumped == true) 
{ 
    this.Transform.MoveIncrement = Vector2.UnitY * timeBetweenUpdates; 
    this.Transform.IsMoved = true; 
    // hasJumped = false; 
} 
if (hasJumped == false) 
{ 
    // Transform.MoveIncrement = new Vector2(0, 0); 
    // Transform.IsMoved = false; 
} 

我将此添加到我的Collision类来解决此问题。

PlayerSprite pSprite = (PlayerSprite)collider; 
      if((collidee is WallSprite) && (collider is PlayerSprite)) 
      { 
       pSprite.hasJumped = false; 
      } 

碰撞和对撞机是精灵这是我如何将其投入PlayerSprite。

+0

您需要检查碰撞情况,当您的玩家碰撞地板时,您需要将hasJumped布尔值设置为false。角色和表面之间相当简单的盒子碰撞会告诉你。您需要确保盒子与您的精灵脚部吻合,否则与平台的碰撞可能会重置布尔值,并且在玩家尚未能完成时允许双跳。 –

+1

http://www.gameprogrammingpatterns.com/state.html < - 使用状态模式的一个特别好的例子。 –

回答

1

正如@Mike McMahon所建议的那样,您需要在碰撞部分执行此操作。

你没有在这里粘贴任何代码,但你需要检查你的玩家的边界矩形是否与你的地面砖相交。如果是这样,你需要设置你的hasJumped等于false,因为他不再跳。

有一个很好的例子叫做Platformer你可以看到这个行为是如何完成的。我建议你下载它。 (Link here)