2013-04-07 55 views
0

我有和XNA 2D遊戲,我一直在做,但我有問題。XNA 2D水平滾動框和碰撞相機

我已經有框在屏幕上滾動,我的精靈正在跳過。精靈之後是一臺2D相機,我擔心相機造成了問題,因爲它會導致滾動框停在屏幕中間而不是繼續,而且生命數量正在迅速下降,而不是一次生命當發生一次碰撞時。

這是在我的精靈與移動箱

Rectangle fairyRectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); 

     hit = false; 

     for (int i = 0; i < GameConstants.TotalBoxes; i++) 
     { 
      if (scrollingBlocks.boxArray[i].alive) 
      { 
       Rectangle blockRectangle = new Rectangle((int)scrollingBlocks.boxArray[i].position.X, (int)scrollingBlocks.boxArray[i].position.Y, scrollingBlocks.boxArray[i].texture.Width, scrollingBlocks.boxArray[i].texture.Height); 

       if (IntersectPixels(fairyRectangle, fairyTextureData, blockRectangle, blockTextureData)) 
       { 
        scrollingBlocks.boxArray[i].alive = false; 



        hit = true; 
        lives--; 

        scrollingBlocks.boxArray[i].alive = true; 

        scrollingBlocks.boxArray[i].position.X = random.Next(GameConstants.ScreenWidth); 
        scrollingBlocks.boxArray[i].position.Y = 570; 
       } 
      } 

     } 

碰撞的代碼,這是在滾動框更新功能 公共無效更新(GameTime gameTime) {

 for (int i = 0; i < GameConstants.TotalBoxes; i++) 
     { 
      boxArray[i].position.X = boxArray[i].position.X - 5; 
      boxArray[i].position.Y = boxArray[i].position.Y; 

      if (boxArray[i].position.X < 0) 
      { 
       boxArray[i].position.X = randomno.Next(GameConstants.ScreenWidth) + 700; 
       boxArray[i].position.Y = 570; 
      } 

      Helper.WrapScreenPosition(ref boxArray[i].position); 
     } 



    } 

我希望他們從右邊的屏幕開始一直移動到x = 0,但他們目前正在x = 400的中途停下。

最後這就是我畫這一切,與精靈和塊

if (gameState == GameState.PLAYINGLEVEL3) 
     { 
      graphics.GraphicsDevice.Clear(Color.Aquamarine); 
      spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
      backgroundManager.Draw(spriteBatch); 
      //scrollingBlocks.Draw(spriteBatch); 
      spriteBatch.DrawString(lucidaConsole, "Score: " + score + " Level: " + level + " Time Remaining: " + ((int)timer/1000).ToString() + " Lives Remaining: " + lives, scorePosition, Color.DarkOrchid); 
      spriteBatch.End(); 


      spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, camera.transform); 
      scrollingBlocks.Draw(spriteBatch); 
      fairyL3.Draw(spriteBatch); 
      spriteBatch.End(); 
     } 

感謝您的幫助!

回答

0

從提供我不能告訴太多瞭解您的相機的作品,但我的猜測是,當您啓動級別相機挑選一個新的中心點(而不是原來的XNA屏幕位置)

在代碼換句話說,相機的中心可能已經將它推回了一點,所以你認爲屏幕的位置0可能已經被推到屏幕的中間使得盒子停止。我建議看看這些相機教程http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/http://www.youtube.com/watch?v=pin8_ZfBgq0

相機可能會很棘手,所以我建議看看幾個教程來獲取掛出矩陣和查看端口。

至於奪走不止一個人的生命,這是因爲你的說法:「如果這個盒子與我的玩家發生碰撞,請帶走生命。」計算機並不知道只要一個人拿走它,只要它與其帶走的玩家相撞,它就會不斷減少。

希望這會給你一些想法:D