2014-09-28 98 views
0

我正在編寫一個遊戲,你必須從一個浮木跳到另一個浮木。你可以看到我這裏有:從網格中的一個方塊跳到另一個方塊

enter image description here

木板產卵在頂部和向下滾動。您可以向上或向左或向右跳一塊拼貼塊和一塊或兩塊拼貼塊。

所有元素(木板和球員,如果他是不是跳躍,站在一塊木板)每移動draw(float delta)被稱爲時間:

setY(getY() + FlotsamGroup.yVelocity * delta); 

這工作得很好,但如果玩家開始跳我要他在這個位置的木板上完全相同的座標上。我的代碼,他不與當時它看起來是這樣的:

enter image description here

你可以看到周圍的玩家的綠線不會受到周圍木板的高度相同,但雙方是正確的,所以它看起來像x軸工作正常。

我想我不知何故必須使用上面的速度代碼的修改版本,但我也希望當木板向下滾動時讓玩家跳得更快,並且總有一天我想包括整個遊戲獲得與木板的你已經成功躍升量更快(減少FlotsamGroup.yVelocity-Value

protected class JumpAction extends Action { 

    // Direction the player wants to jump to 
    Direction direction; 
    // The player actor 
    Player player; 
    // scalarVector is the vector, which has to be added to players position 
    Vector2 scalarVector; 
    // targetPosition is the players position add with the scalarVector 
    Vector2 targetPosition; 

    public JumpAction(Direction direction) { 
     this.direction = direction; 
     Gdx.app.log("", "Go to " + direction); 

     /* Everytime we need to jump one tile up, so Y from scalarVector is everytime 1 
      How much we have to go on x-axis dependens on the direction */ 
     scalarVector = new Vector2(0, 1); 
     switch(direction) { 
     case TWOLEFT: 
      scalarVector.x = -2; 
      break; 
     case LEFT: 
      scalarVector.x = -1; 
      break; 
     case UP: 
      break; 
     case RIGTH: 
      scalarVector.x = 1; 
      break; 
     case TWORIGTH: 
      scalarVector.x = 2; 
      break; 
     } 
    } 

    @Override 
    public boolean act(float delta) { 
     if(player == null) { 
      player = (Player) getActor(); 
      player.isJumping = true; 
      targetPosition = new Vector2(player.getX() + GameGrid.getSquareSize() * scalarVector.x, player.getY() + GameGrid.getSquareSize() * scalarVector.y); 
      Gdx.app.log("", "Target Vector " + scalarVector.toString() + " | Target Position " + targetPosition.toString()); 
     } 

     // Somehow modify players position to smoothly move him to his target   
     player.setX(player.getX() + (scalarVector.x/-(FlotsamGroup.yVelocity * delta))); 
     player.setY(player.getY() + (scalarVector.y/-(FlotsamGroup.yVelocity * delta))); 

     // Check if player has been moved enough to stand on the new plank 
     switch(direction) { 
     case TWOLEFT: 
     case LEFT: 
      if(player.getX() < targetPosition.x) 
       player.isJumping = false; 
      break; 
     case UP: 
      if(player.getY() > targetPosition.y) 
       player.isJumping = false; 
      break; 
     case RIGTH: 
     case TWORIGTH: 
      if(player.getX() > targetPosition.x) 
       player.isJumping = false; 
      break; 
     } 

     if(!player.isJumping) { 
      // Player isn't jumping anymore, now we can set his position to target position 
      player.setX(MathUtils.roundPositive(targetPosition.x)); 
      player.setY(MathUtils.roundPositive(targetPosition.y)); 
      // Return true to remove this action from the player-actor 
      return true; 
     } 
     return false; 
    } 
} 
+0

究竟是你問?你的代碼是沒有記錄的,我沒有看到任何瓷磚地圖或木瓦列表...還有什麼是scalarVector oxymoron?你的運動應該平穩或者與網格步驟?玩家位置在哪裏? – Spektre 2014-09-29 06:54:55

+0

我問我該如何讓玩家順利移動到「方向」表示他想跳到的瓦片的正中間,因此它看起來不像第二張圖片。我現在也記錄了我的代碼。你可以用'player.getX();'或'player.getY();'來獲得玩家位置,但是我認爲你不需要所有木片的列表,因爲你知道所有木頭的速度(' FlotsamGroup.yVelocity')@Spektre – elterPro 2014-09-29 11:18:06

+0

你可能需要做一些像'player.setY(MathUtils.roundPositive(targetPosition.y - targetPosition.width))' – 2014-09-29 17:06:06

回答

0

我會改變這樣的:

// Somehow modify players position to smoothly move him to his target   
    player.setX(player.getX() + (scalarVector.x/-(FlotsamGroup.yVelocity * delta))); 
    player.setY(player.getY() + (scalarVector.y/-(FlotsamGroup.yVelocity * delta))); 

對此(C++):

// needed change in position 
    float dx=targetPosition.getX()-player.getX(); 
    float dy=targetPosition.getY()-player.getY(); 
    // make it unit vector 
    float q=sqrt((dx*dx)+(dy*dy)); 
    float JumpSpeed=???; // here add some constant or variable jump speed you can use value of q for accelleration/breaking decision... 
    if (q<1e-3) q=0.0; //here handle player reached target... 
    else q=1.0/q; 
    dx*=q; dy*=q; 
    // update position 
    player.setX(player.getX()+(dx*JumpSpeed*delta)); 
    player.setY(player.getY()+(dy*JumpSpeed*delta)); 
    // here add the flow speed position update... 
  • targetPosition應持有實際的目標位置(因此也與流速修改...)
  • 它只是修改播放器和目標位置之間的線性插值...
相關問題