2014-01-11 88 views
0

我有阻擋運動的問題,我已經閱讀Blocking Movement On Collision 的問題,但我對自己的問題沒有任何想法。 如果你可以給我邏輯我會盡力解決我自己的問題。 我希望你能幫助我 這是我的播放器類更新碰撞塊運動

:編輯:感謝凱哈特曼答覆 我的問題是2D圖形,我想知道如何停止運動時object1碰撞對象2

public void Update(GameTime gameTime) 
{ 
    // Ani Test 
    up_ani.Update(gameTime); 
    down_ani.Update(gameTime); 
    left_ani.Update(gameTime); 
    right_ani.Update(gameTime); 

    position += velocity; 

    if (Keyboard.GetState().IsKeyDown(key.MoveUp) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     currentFace = FacePosition.Up; 
     velocity.Y = -3; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     currentFace = FacePosition.Down; 
     velocity.Y = 3; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveRight) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     currentFace = FacePosition.Right; 
     velocity.X = 3; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveLeft) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveUp)) 
    { 
     currentFace = FacePosition.Left; 
     velocity.X = -3; 
    } 
    else 
    { 
     //currentFace = FacePosition.Down; 
     velocity = Vector2.Zero; 
    } 

    prevState = Keyboard.GetState(); 
} 

,這我碰撞

public void IntersectWithScore(Vector2 vector1, Vector2 vector2, Texture2D object1, Texture2D object2, int doSomethingToScore, bool isIncrease) 
{ 
    if (vector1.X + object1.Width < vector2.X || vector1.X > vector2.X + object2.Width || 
    vector1.Y + object1.Height < vector2.Y || vector1.Y > vector2.Y + object2.Height) 
    { 

    } 
    else 
    { 
     player1.Velocity = Vector2.Zero; 
    } 
} 
+0

你沒有說出你的問題到底是什麼。此外,這是3D還是2D? –

回答

0

無需碰撞檢查方法就我而言:)我寫這段代碼的確很快,但應該工作好嗎?(ISH)。我假設你想檢查玩家的方式是否有問題,並且假定有多個對象可能會以你的玩家的方式進行,所以我建議爲「固體」對象製作一個課程並給它一個位置,寬度和高度。然後讓這個類的列表包含多個「實體」對象。

// collidable object's Class name = SolidBlock 
SolidBlock newSolidBlock; 
List<SolidBlock> solidBlocksList = new List<SolidBlock>(); 

// This will create 10 'solid' objects and adds them all to a list 
// This would preferably go in the Load() function so you can set the positions of 
// the blocks after creating them (after the loop) 
int i = 0; 
while (i < 10) 
{ 
    newSolidBlock = new SolidBlock(Vector2, position) // Assuming width and height 
                 // are set within class 
    solidBlocksList.Add(newSolidBlock); 
    i++; 
} 

// It doesn't matter how you create them, the important lines here are the creation of a 
// newSolidBlock and then the line adding it to the list of blocks. 

solidBlocksList[0].position = new Vector (20, 50); // < for example this is how you would set 
                // a block's position. 

然後在您的玩家更新功能中,您可以引用這個對象列表。

// Input a list of the objects you want your player to collide with 
// this is a list in case you have multiple object2s you want to check 
// for collision. 
// An Object2 should have a position, height, and width, yes? 

public void Update(GameTime gameTime, List<Object2> object2List) 
{ 
    // Ani Test 
    up_ani.Update(gameTime); 
    down_ani.Update(gameTime); 
    left_ani.Update(gameTime); 
    right_ani.Update(gameTime); 

    position += velocity; 

    if (Keyboard.GetState().IsKeyDown(key.MoveUp) && prevState.IsKeyUp(key.MoveDown) &&  prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     foreach (Object2 o in object2List) 
     { 
      if (position.Y <= o.position.Y + o.height) 
      { 
       velocity.Y = 0; 
      } 
      else 
      { 
       velocity.Y = -3; 
      } 
     } 
     currentFace = FacePosition.Up; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) &&  prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     foreach (Object2 o in object2List) 
     { 
      if (position.Y + playerWidth >= o.position.Y) 
      { 
       velocity.Y = 0; 
      } 
      else 
      { 
       velocity.Y = 3; 
      } 
     } 
     currentFace = FacePosition.Down; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveRight) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveUp) && prevState.IsKeyUp(key.MoveLeft)) 
    { 
     foreach (Object2 o in object2List) 
     { 
      if (position.X + playerWidth >= o.position.X) 
      { 
       velocity.X = 0; 
      } 
      else 
      { 
       velocity.X = 3; 
      } 
     } 
     currentFace = FacePosition.Right; 
    } 
    else if (Keyboard.GetState().IsKeyDown(key.MoveLeft) && prevState.IsKeyUp(key.MoveDown) && prevState.IsKeyUp(key.MoveRight) && prevState.IsKeyUp(key.MoveUp)) 
    { 
     foreach (Object2 o in object2List) 
     { 
      if (position.X <= o.position.X + o.width) 
      { 
       velocity.X = 0; 
      } 
      else 
      { 
       velocity.X = -3; 
      } 
     } 
     currentFace = FacePosition.Left; 
    } 
    else 
    { 
     velocity = Vector2.Zero; 
    } 

    prevState = Keyboard.GetState(); 
} 

很抱歉,如果這是沒有太大的幫助,尤其是第一個代碼塊,這是非常多的,你怎麼能創建多個object2s一個例子。