2013-04-15 1 views
2

XNA를 사용하여 공간 사수 게임을 진행하고 있으며 여러 개의 튜토리얼을 따라 시차 배경을 만들었습니다. 지금까지 X 또는 Y 중 하나의 축을 따라 갈 수 있지만 둘 다 동시에 할 수는 없습니다. 저는 플레이어를 따라 움직이는 카메라 클래스를 가지고 있습니다. 플레이어 클래스는 플레이어를 움직이게하는 대신 플레이어를 움직이는 것이 더 쉬울 것이라고 생각했기 때문에 플레이어가 움직입니다 ('세계'대신에).XNA와 C#을 사용하여 X 축과 Y 축 모두에서 2D 시차 배경을 사용했습니다.

지금까지 배경은 플레이어를 따라갈 수 없으며 동시에 두 축을 모두 이해할 수도 없습니다. 나는 타일 엔진에 대해 생각했지만, 다른 시차로 시차를 두지 않을 것입니까?

내가해야 할 일을 이해하는 데 도움이되는 사람이 있습니까? 아니면 동시에 두 축을 수행 할 수있는 자습서를 추천 할 수 있습니까? 이번에는 내 자신의 대답을 찾을 수없는 것 같습니다.

class Background 
{ 
    // Textures to hold the two background images 
    Texture2D spaceBackground, starsParallax; 

    int backgroundWidth = 2048; 
    int backgroundHeight = 2048; 

    int parallaxWidth = 2048; 
    int parallaxHeight = 2048; 

    int backgroundWidthOffset; 
    int backgroundHeightOffset; 

    int parallaxWidthOffset; 
    int parallaxHeightOffset; 

    public int BackgroundWidthOffset 
    { 
     get { return backgroundWidthOffset; } 
     set 
     { 
      backgroundWidthOffset = value; 
      if (backgroundWidthOffset < 0) 
      { 
       backgroundWidthOffset += backgroundWidth; 
      } 
      if (backgroundWidthOffset > backgroundWidth) 
      { 
       backgroundWidthOffset -= backgroundWidth; 
      } 
     } 
    } 

    public int BackgroundHeightOffset 
    { 
     get { return backgroundHeightOffset; } 
     set 
     { 
      backgroundHeightOffset = value; 
      if (backgroundHeightOffset < 0) 
      { 
       backgroundHeightOffset += backgroundHeight; 
      } 
      if (backgroundHeightOffset > backgroundHeight) 
      { 
       backgroundHeightOffset -= backgroundHeight; 
      } 
     } 
    } 

    public int ParallaxWidthOffset 
    { 
     get { return parallaxWidthOffset; } 
     set 
     { 
      parallaxWidthOffset = value; 
      if (parallaxWidthOffset < 0) 
      { 
       parallaxWidthOffset += parallaxWidth; 
      } 
      if (parallaxWidthOffset > parallaxWidth) 
      { 
       parallaxWidthOffset -= parallaxWidth; 
      } 
     } 
    } 

    public int ParallaxHeightOffset 
    { 
     get { return parallaxHeightOffset; } 
     set 
     { 
      parallaxHeightOffset = value; 
      if (parallaxHeightOffset < 0) 
      { 
       parallaxHeightOffset += parallaxHeight; 
      } 
      if (parallaxHeightOffset > parallaxHeight) 
      { 
       parallaxHeightOffset -= parallaxHeight; 
      } 
     } 
    } 

    // Constructor when passed a Content Manager and two strings 
    public Background(ContentManager content, 
         string sBackground, string sParallax) 
    { 
     spaceBackground = content.Load<Texture2D>(sBackground); 
     backgroundWidth = spaceBackground.Width; 
     backgroundHeight = spaceBackground.Height; 

     starsParallax = content.Load<Texture2D>(sParallax); 
     parallaxWidth = starsParallax.Width; 
     parallaxHeight = starsParallax.Height; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     // Draw the background panel, offset by the player's location 
     spriteBatch.Draw(
      spaceBackground, 
      new Rectangle(-1 * backgroundWidthOffset, 
          -1 * backgroundHeightOffset, 
          backgroundWidth, 
          backgroundHeight), 
      Color.White); 

     // If the right edge of the background panel will end 
     // within the bounds of the display, draw a second copy 
     // of the background at that location. 
     if (backgroundWidthOffset > backgroundWidth) 
     { 
      spriteBatch.Draw(
       spaceBackground, 
       new Rectangle(
        (-1 * backgroundWidthOffset) + backgroundWidth, 0, 
        backgroundWidth, backgroundHeight), 
       Color.White); 
     } 
     else //(backgroundHeightOffset > backgroundHeight - viewportHeight) 
     { 
      spriteBatch.Draw(
       spaceBackground, 
       new Rectangle(
        0, (-1 * backgroundHeightOffset) + backgroundHeight, 
        backgroundHeight, backgroundHeight), 
       Color.White); 
     } 

     // Draw the parallax star field 
     spriteBatch.Draw(
      starsParallax, 
      new Rectangle(-1 * parallaxWidthOffset, 
          0, parallaxWidth, 
          parallaxHeight), 
      Color.SlateGray); 
     // if the player is past the point where the star 
     // field will end on the active screen we need 
     // to draw a second copy of it to cover the 
     // remaining screen area. 
     if (parallaxWidthOffset > parallaxWidth) 
     { 
      spriteBatch.Draw(
       starsParallax, 
       new Rectangle(
        (-1 * parallaxWidthOffset) + parallaxWidth, 
        0, 
        parallaxWidth, 
        parallaxHeight), 
       Color.White); 
     } 
    } 
} 

다음 플레이어와 함께 배경을 이동하는 관계를 맺고 주요 게임 : 여기

내 배경 클래스에 대한 코드입니다.

background.BackgroundWidthOffset -= 2; 
background.ParallaxWidthOffset -= 1; 

은 시각적, 배경은 일종의 불안해하고, 무작위로 건너 뛰거나 배경 타일을 겹쳐 보인다.

답변

1

나는 좋은 결과와 함께 과거에이 방법을 사용했습니다 :

http://www.david-gouveia.com/scrolling-textures-with-zoom-and-rotation/

그것은 빠른 구현의 결과로 효과를 달성하기 위해 셰이더를 사용합니다.

+1

감사합니다. 이해하기 쉽고 구현하기가 쉽습니다. 나는 방금 시차를 만들기 위해 투명한 배경을 사용했다. 완벽하게 작동합니다! – user2280848

+0

깨진 링크. 제발 업데이 트 – Relic

+0

나는 내 사이트가 아니라 깨진 링크를 수정할 수 없습니다. – jgallant

0

완전한 예가 here입니다.

+0

깨진 링크 ... Microsoft에 광고하려고하지 않는 한 :/ – Relic

+0

pdf를 다운로드하고 "Platformer : 스크롤링 레벨 추가"검색 – jlvaquero

+0

그리고 사용자가 XNA를 요청할 때 Microsoft에 대한 링크가 문맥 광고를 무료로 그리고 밖으로 ... – jlvaquero