0
A
답변
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);
}
관련 문제
- 1. XNA 회전 및 이동 스프라이트
- 2. 스프라이트 키트로 배경 이동
- 3. XNA Windows Phone 7 스프라이트 이동
- 4. 스프라이트, XNA, C# 및 우아함
- 5. XNA 스트레칭 스프라이트
- 6. 스프라이트 회전 XNA
- 7. XNA 스프라이트 프레임 불일치
- 8. XNA 스프라이트 회전 지점
- 9. XNA 스프라이트 정렬 모드
- 10. XNA 알파와 프리미티브 및 스프라이트 블렌딩
- 11. CSS 스프라이트 배경 및 선택자
- 12. 스프라이트 이동 및 이벤트 처리
- 13. 스프라이트 배경
- 14. 2D XNA 카메라 및 고정 배경
- 15. 공백 및 배경 이동
- 16. ArrayController 캐싱 및 워킹
- 17. XNA - 2D 스프라이트 확대/축소
- 18. XNA 충돌 후 스프라이트 제거
- 19. 투명도에서 불투명도의 XNA 애니메이션 스프라이트
- 20. XNA 타일 맵에 스프라이트 맞추기
- 21. XNA 칼 스프라이트 충돌 감지
- 22. XNA : 회전하는 스프라이트 간의 충돌
- 23. XNA, 스프라이트 애니메이션에 각도 그리기
- 24. XNA spriteBatch.Draw - 깜박 거리는 스프라이트
- 25. XNA 스프라이트 뒤에 색상을 얻을 수있는 방법
- 26. XNA 벡터 수학 이동
- 27. CSS 스프라이트 시트 이동 전환. 배경 이미지를 미끄러지지 않고 편안하게하십시오.
- 28. Node.js/Nowjs - 스프라이트 이동 및 서버 측 시간 추적
- 29. 스프라이트 스크롤링 루핑 배경
- 30. XNA 게임 스튜디오 가장 간단한 스프라이트 애니메이션
... 배경 – pravin
이동합니다하지만 그 상황에서 문자는 항상 같은 위치에있을 것입니다. – pinker
그런 다음 배경 X에서 값을 빼서 왼쪽으로 이동하십시오. 플레이어의 X 방향과 반대 방향으로 움직이면 왼쪽으로도 움직일 수 있습니다. amountOfPlayerMovement * -1 * backgroundMoveSpeed * (부동) gameTime.ElapsedGameTime.TotalSeconds; – Beanish