2013-08-09 3 views
1

저는 Platformer Starter Kit로 놀고 있으며 지금까지 수평 및 수직 "카메라"움직임과 여우를 시차 배경으로 추가하려고했습니다. 문제는 두 개의 배경 레이어 이후에 나머지 레이어가 보이지 않게된다는 것입니다. 임 아주 XNA에 새로운 도움이 필요합니다 :). 문제를 heres 그림 : Heres는XNA 시차 배경은 모든 레이어를 표시하지 않습니다

layer fail

코드. 당신이 좀 더 :)

레이어 수업을 필요로하는 경우에 저에게 알려주십시오 :

class Layer 
{ 
    public Texture2D[] Textures { get; private set; } 
    public float ScrollRate { get; private set; } 

    public Layer(ContentManager content, string basePath, float scrollRate) 
    { 
     // Assumes each layer only has 3 segments. 
     Textures = new Texture2D[3]; 
     for (int i = 0; i < 3; ++i) 
      Textures[i] = content.Load<Texture2D>(basePath + "_" + i); 

     ScrollRate = scrollRate; 
    } 

    public void Draw(SpriteBatch spriteBatch, float cameraPosition, float cameraPositionYAxis) 
    { 
     // Assume each segment is the same width. 
     int segmentWidth = Textures[0].Width; 

     // Calculate which segments to draw and how much to offset them. 
     float x = cameraPosition * ScrollRate; 
     float y = ScrollRate; 
     int leftSegment = (int)Math.Floor(x/segmentWidth); 
     int rightSegment = leftSegment + 1; 
     x = (x/segmentWidth - leftSegment) * -segmentWidth; 

     spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, -y), Color.White); 
     spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, -y), Color.White); 
    } 


} 

Heres는 내 ScrollCamera 내 Level.cs에서 무승부 방법

을 (ScrollCamera 그것과 아무 상관이 있는지 잘 모릅니다)
public void Draw(GameTime gameTime, SpriteBatch spriteBatch) 
    { 
     ScrollCamera(spriteBatch.GraphicsDevice.Viewport); 
     Matrix cameraTransformYAxis = Matrix.CreateTranslation(-cameraPosition, -cameraPositionYAxis, 0.0f); 
     spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, 
      DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, cameraTransformYAxis); 

     //added this foreach loop 
     foreach (var layer in layers) 
     { 
      layer.Draw(spriteBatch, cameraPosition, cameraPositionYAxis); 
     } 

     DrawTiles(spriteBatch); 
     Player.Draw(gameTime, spriteBatch); 
     foreach (Enemy enemy in enemies) 
     { 
      enemy.Draw(gameTime, spriteBatch); 
     } 

     spriteBatch.End(); 

    } 

    private void ScrollCamera(Viewport viewport) 
    { 
     #if ZUNE 
     const float ViewMargin = 0.4f; 
     #else 
     const float ViewMargin = 0.5f; 
     #endif 

     float marginWidth = viewport.Width * ViewMargin; 
     float marginLeft = cameraPosition + marginWidth; 
     float marginRight = cameraPosition + viewport.Width - marginWidth; 


     const float TopMargin = 0.4f; 
     const float BottomMargin = 0.4f; 
     float marginTop = cameraPositionYAxis + viewport.Height * TopMargin; 
     float marginBottom = cameraPositionYAxis + viewport.Height - viewport.Height * BottomMargin; 
     // float maxCameraPositionYOffset = Tile.Height * Height - viewport.Height; 


     float CameraMovement = 0.0f; 
     if (Player.Position.X < marginLeft) 
      CameraMovement = Player.Position.X - marginLeft; 
     else if (Player.Position.X > marginRight) 
      CameraMovement = Player.Position.X - marginRight; 
     //Aktualizuj przesuwanie ekranu, ale zapobiegnij wyjściu poza mape 
     float maxCameraPosition = Tile.Width * Width - viewport.Width; 
     cameraPosition = MathHelper.Clamp(cameraPosition + CameraMovement, 0.0f, maxCameraPosition); 



     float cameraMovementY = 0.0f; 
     if (Player.Position.Y < marginTop) //above the top margin 
      cameraMovementY = Player.Position.Y - marginTop; 
     else if (Player.Position.Y > marginBottom) //below the bottom margin 
      cameraMovementY = Player.Position.Y - marginBottom; 

     float maxCameraPositionYOffset = Tile.Height * Height - viewport.Height; 
     cameraPositionYAxis = MathHelper.Clamp(cameraPositionYAxis + cameraMovementY, 0.0f, maxCameraPositionYOffset); 

    } 

그리고 나는 그렇게 생각한다. 좀 더 코드가 필요하면 말해주십시오 :)

답변

1

선형 포장을 사용하고 싶습니다. There's an excellent blog post on it right here. 이것은 물론 텍스쳐 타일이 완벽하다고 가정합니다. 응답에 대한

// Use this one instead! 
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); 
spriteBatch.Draw(texture, position, new Rectangle(-scrollX, -scrollY, texture.Width, texture.Height), Color.White); 
spriteBatch.End(); 
+0

감사합니다 당신이 아주 간단 보이지만, 내 프로젝트에이를 삽입하는 방법에 임 정말 확실하지 난 아주 그림을 couldnt : 당신은 단지 아래 코드 예제, 당신의 선형 배치 모드를 설정할 필요가 내 프로젝트에 이것을 삽입하는 방법에 대해 알아보십시오. 그것은 제가 그리기 방법에 넣은 것처럼 간단합니까? 모든 것이 작동합니다 : P? –

+0

타일링을 그릴 때 SpriteBatch 시작 호출을 설정 한대로 설정하고 일반적인 것처럼 그립니다. 명심해라, 단지 타일을 붙인 재료를 위해 이것을해라. 래핑하지 않으려는 모든 작업에 대해 새 배치를 시작해야합니다. 그 외에는 네. 자동입니다. –

+0

나는 아직도 그것을 이해할 수 없다. Level.cs 또는 다른 곳의 Draw 메서드에서이 작업을 수행합니까? 정확히 어떻게? 죄송합니다. Im이 어리석은 질문을하는 경우. : P –

관련 문제