2014-04-02 3 views
0

XNA에서 캐릭터가 오른쪽으로 움직일 때 배경이 움직이는 간단한 작업을하고 싶습니다.스프라이트 워킹 및 배경 이동 : XNA

아이디어가 있습니까?

감사합니다.

+0

... 배경 – pravin

+0

이동합니다하지만 그 상황에서 문자는 항상 같은 위치에있을 것입니다. – pinker

+1

그런 다음 배경 X에서 값을 빼서 왼쪽으로 이동하십시오. 플레이어의 X 방향과 반대 방향으로 움직이면 왼쪽으로도 움직일 수 있습니다. amountOfPlayerMovement * -1 * backgroundMoveSpeed ​​* (부동) gameTime.ElapsedGameTime.TotalSeconds; – Beanish

답변

2

게임 마리오처럼 생각하세요! 스크롤 사용.

게임 클래스를 만듭니다. 스프라이트 그리기 절차에서 설명한대로 자원을로드하십시오. 배경 텍스처를로드하십시오.

private ScrollingBackground myBackground; 

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 
    myBackground = new ScrollingBackground(); 
    Texture2D background = Content.Load<Texture2D>("starfield"); 
    myBackground.Load(GraphicsDevice, background); 
} 

배경 텍스처의 크기와 화면 크기를 결정하십시오.

텍스처 크기는 Height 및 Width 속성을 사용하여 결정되며 화면 크기는 그래픽 장치의 Viewport 속성을 사용하여 결정됩니다.

텍스처 및 화면 정보를 사용하여 텍스처의 원점을 텍스처의 상단 가장자리의 가운데로 설정하고 초기 화면 위치를 화면 가운데로 설정합니다.

// class ScrollingBackground 
private Vector2 screenpos, origin, texturesize; 
private Texture2D mytexture; 
private int screenheight; 
public void Load(GraphicsDevice device, Texture2D backgroundTexture) 
{ 
    mytexture = backgroundTexture; 
    screenheight = device.Viewport.Height; 
    int screenwidth = device.Viewport.Width; 
    // Set the origin so that we're drawing from the 
    // center of the top edge. 
    origin = new Vector2(mytexture.Width/2, 0); 
    // Set the screen position to the center of the screen. 
    screenpos = new Vector2(screenwidth/2, screenheight/2); 
    // Offset to draw the second texture, when necessary. 
    texturesize = new Vector2(0, mytexture.Height); 
} 

배경을 스크롤하려면 Update 메서드에서 배경 텍스처의 화면 위치를 변경하십시오. 이 예제는 화면 위치의 Y 값을 증가시켜 초당 100 픽셀을 배경으로 이동합니다.

protected override void Update(GameTime gameTime) 
{ 
    ... 
    // The time since Update was called last. 
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; 

    // TODO: Add your game logic here. 
    myBackground.Update(elapsed * 100); 

    base.Update(gameTime); 
} 

는 Y 값 되돌아 가기 화면 하단에서 배경 스크롤하기, 텍스쳐 높이보다 더 크게 유지되지 않는다.

public void Update(float deltaY) 
{ 
    screenpos.Y += deltaY; 
    screenpos.Y = screenpos.Y % mytexture.Height; 
} 
// ScrollingBackground.Draw 

LoadContent 및 업데이트에서 계산 된 원본 및 화면 위치를 사용하여 배경을 그립니다.

protected override void Draw(GameTime gameTime) 
{ 
    GraphicsDevice.Clear(Color.CornflowerBlue); 

    spriteBatch.Begin(); 
    myBackground.Draw(spriteBatch); 
    spriteBatch.End(); 

    base.Draw(gameTime); 
} 

질감이 화면을 가리지 않는 경우 다른 질감이 그려집니다. 이는로드 할 때 생성 된 텍스처 생성 벡터를 사용하여 화면 위치에서 텍스처 높이를 뺍니다. 이것은 루프의 환상을 만듭니다. 당신이 배경 오른쪽 increament의 x에 문자를 이동할 때

public void Draw(SpriteBatch batch) 
{ 
    // Draw the texture, if it is still onscreen. 
    if (screenpos.Y < screenheight) 
    { 
     batch.Draw(mytexture, screenpos, null, 
      Color.White, 0, origin, 1, SpriteEffects.None, 0f); 
    } 
    // Draw the texture a second time, behind the first, 
    // to create the scrolling illusion. 
    batch.Draw(mytexture, screenpos - texturesize, null, 
     Color.White, 0, origin, 1, SpriteEffects.None, 0f); 
}