2012-10-23 157 views
4

我正在创建2D马里奥游戏。游戏跳转逻辑

以下功能旨在当按下特定按键来更新玩家的位置。玩家可以左右移动,跳到同一个地方,或跳到左边或右边(形成弧形)。

bool updatePlayerPosition(Movement* mov){ 
     if (this->keyPressed(SDLK_RIGHT)) { 
      mov->applyForce(1); // Changes the velocity in X 
     } 
     if (this->keyPressed(SDLK_LEFT)) { 
      mov->applyForce(-1); // Changes the velocity in X 
     }   
     if (this->keyPressed(SDLK_SPACE)) { 
      mov->jump();  // Changes the velocity in Y 
     }  
     if (this->keyPressed(SDLK_DOWN)) { 
      mov->fallDown(); // Changes the velocity in X and Y 
     } 

     Point* pos = mov->getPosition(); 

     // Check whether the position is out of bounds 
     if(Level::allowsMove(pos)){ 
       // If it is not, I update the player's current position 
       position->x = pos->x; 
       position->y = pos->y; 
       return true; 
     } 
     // If the movement is not allowed, I don't change the position 
     else { 
       mov->setPosition(*position); 
       return false; 
     } 
    } 

这里是错误:当我打的水平(其中有一个固定的宽度)的结束,如果我试图向右移动,并在同一时间跳,玩家跳跃,并保持在空气。只有当我释放空格键时,玩家才会落地。

我该如何解决这个问题?

+0

一个建议:如果'是持久mov'不是必需的,只是把它定义在函数内部。 –

回答

2

对于您的游戏,我认为您只希望玩家在按下空间时跳跃当玩家在地板上时。然后你必须检查球员是否在场上以获得所需的行为。

我建议你设备的机制是这样的:

if (this->keyPressed(SDLK_SPACE) && this->isOnTheFloor()) { 
           ^^^^^^^^^^^^^^^^^^^^^^^ 
    mov->jump();  // Changes the velocity in Y 
}  
+0

'keyPressed(X)'返回'true'是键X被按下。 –

0

你的空格键处理程序应只适用力一次 - 上键不放,或向上,如果你喜欢,不是每一个帧。在空格键下,速度'上'应该被设置为一些(可能是恒定的)值。然后,如果不是在地面上,则每个框架都应该以最大速度增加指定量。所以OnSpacebarDown, YVelocity = 10.0;并为每个帧if(!bGrounded) YVelocity -= 1.0;