I'm trying to write a 2D vertical scrolling background for a shooter. My implementation is a bit different. I have one BIG texture (1600X4194). Game screen resolution is 1024X768.
我尝试着为我的射击游戏开发一个2D的垂直滚动背景,不过我的实现方法有点另类,呵呵,我使用了一个大背景(1600X4194). 游戏屏幕的分辨率是1024X768.。
I start by drawing a rectangle the size of the game window at the bottom of this texture and slowly move that rectangle up until it get to the top then I wrap. It works for the most part except I have one weird issue. Take a look at this line:
游戏的实现方法是在游戏里画出哪个大背景图片的一部分,接着慢慢的移动,当移动到尽头的时候再调头重复画,周而复始。用这种方法几乎没有太大问题,不过有个奇怪的问题,请看下面一句:
th = mytextureinfo.Height / 2 - (screenheight/12);
I would think I could just do:
我想我是否应该使用下面的代码呢?
th = mytextureinfo.Height;
But that doesn't work. At the start of the game I'm definitely drawing a rectangle outside the bounds of my texture. Why is that? I'm using origin (0,0). I initialize y = mytextureinfo.Height - screenheight. Any ideas?Here is the complete class:
但是问题依然存在。在游戏一开始,我画的矩形背景超出那个大背景图片(1600X4194)的边界. 到底怎么回事? 请问各位有什么建议吗? 下面是具体的实现代码,请各位推敲改进。
public class ScrollingBackground
{
public int speed = 1;
private Texture2D mytexture;
private TextureInformation mytextureinfo;
private int screenheight, screenwidth;
private Rectangle frame = new Rectangle();
private Rectangle frame2 = new Rectangle();
private Vector2 Pos = new Vector2(0f, 0f);
int x, y;
int tw, th;
// ScrollingBackground.Load
public void Load(GraphicsDevice Device, string TextureName)
{
mytextureinfo = Texture2D.GetTextureInformation(TextureName);
mytexture = Texture2D.FromFile(Device, TextureName);
screenheight = Device.Viewport.Height;
screenwidth = Device.Viewport.Width;
tw = mytextureinfo.Width;
th = mytextureinfo.Height / 2 - (screenheight/12);
Reset();
}
public void Reset()
{
x = (tw / 2) - (screenwidth / 2);
y = th - screenheight ;
frame.X = x;
frame.Y = y;
frame.Width = screenwidth;
frame.Height = screenheight;
frame2.X = x;
frame2.Y = y;
frame2.Width = screenwidth;
frame2.Height = screenheight;
}
// ScrollingBackground.Update
public void Update()
{
y -= speed;
if (y < screenheight * -1)
{
Reset();
return;
}
frame.Y = y;
if (y < 0)
{
frame2.Y = th + y;
frame2.Height = y * -1;
}
}
// ScrollingBackground.Draw
public void Draw(SpriteBatch Batch)
{
// Draw the first texture
Batch.Draw(this.mytexture, Pos, this.frame, Color.White);
if (y < 0)
{
//draw second texture
Batch.Draw(this.mytexture, Pos, frame2, Color.White);
}
}
} |