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);
}
確定背景紋理的大小和屏幕的大小。
使用「高度」和「寬度」屬性確定紋理大小,並使用圖形設備上的「視口」屬性確定屏幕大小。
使用紋理和屏幕信息,將紋理的原點設置爲紋理頂邊的中心,並將初始屏幕位置設置爲屏幕中心。
// 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和Update中計算的原點和屏幕位置繪製背景。
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
myBackground.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
如果紋理不覆蓋屏幕,將繪製另一個紋理。這使用加載時創建的紋理矢量從屏幕位置減去紋理高度。這產生了循環的幻覺。
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. 雪碧背景
- 2. XNA旋轉和移動雪碧
- 3. XNA雪碧轉換
- 4. 雪碧旋轉XNA
- 5. 雪碧不移動
- 6. XNA雪碧幀不一致
- 7. XNA雪碧的可見性
- 8. 雪碧不能移動JPanel
- 9. Cocos2d雪碧移動速度
- 10. 裁剪背景圖像雪碧
- 11. 用雪碧重複背景圖片
- 12. 響應雪碧背景圖像,如何
- 13. Python:透明雪碧againt背景
- 14. XNA drawRectangle.X + = 1不會移動我的矩形/雪碧
- 15. CSS雪碧整頁背景:背景位置
- 16. 雪碧運動
- 17. 在移動引擎時動畫雪碧
- 18. CSS背景大小的SVG雪碧:100%自動;
- 19. XNA Windows Phone 7雪碧機芯
- 20. XNA雪碧 - 找不到文件
- 21. XNA二維矩形雪碧粘接盒
- 22. XNA滾動背景
- 23. 在PyGame中移動一個雪碧
- 24. 如何Pygame的移動雪碧
- 25. 雪碧無法正常移動的Java
- 26. 雪碧不能正常移動。 libgdx
- 27. CSS雪碧動畫
- 28. Android,雪碧動畫
- 29. CAKeyframeAnimation雪碧動畫
- 30. 雪碧和福利
當你移動的字符背景的權increament X ...背景將移動 – pravin
,但在這種情況下該角色會始終在同一位置。 – pinker
然後從背景X中減去一個值,然後向左移動。更好的是,如果您將其表現爲玩家的相反X方向,向左移動也會起作用! amountOfPlayerMovement * -1 * backgroundMoveSpeed *(float)gameTime.ElapsedGameTime.TotalSeconds; – Beanish