背景垂直滚动另类实现
<span>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.<br/><font style="BACKGROUND-COLOR: #ffcccc;">我尝试着为我的射击游戏开发一个2D的垂直滚动背景,不过我的实现方法有点另类,呵呵,我使用了一个大背景(1600X4194). 游戏屏幕的分辨率是1024X768.。</font><br/><br/>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:<br/><font style="BACKGROUND-COLOR: #ffcccc;">游戏的实现方法是在游戏里画出哪个大背景图片的一部分,接着慢慢的移动,当移动到尽头的时候再调头重复画,周而复始。用这种方法几乎没有太大问题,不过有个奇怪的问题,请看下面一句:</font><br/>th = mytextureinfo.Height / 2 - (screenheight/12);<br/><br/>I would think I could just do:<br/><font style="BACKGROUND-COLOR: #ffcccc;">我想我是否应该使用下面的代码呢?</font><br/>th = mytextureinfo.Height;<br/><br/>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:<br/><br/><font style="BACKGROUND-COLOR: #ffcccc;">但是问题依然存在。在游戏一开始,我画的矩形背景超出那个大背景图片(1600X4194)的边界. 到底怎么回事? 请问各位有什么建议吗? 下面是具体的实现代码,请各位推敲改进。</font><br/><br/> public class ScrollingBackground<br/> {<br/> public int speed = 1;<br/> private Texture2D mytexture;<br/> private TextureInformation mytextureinfo;<br/> private int screenheight, screenwidth;<br/> private Rectangle frame = new Rectangle();<br/> private Rectangle frame2 = new Rectangle();<br/> private Vector2 Pos = new Vector2(0f, 0f);<br/> int x, y;<br/> int tw, th;<br/><br/> // ScrollingBackground.Load<br/> public void Load(GraphicsDevice Device, string TextureName)<br/> {<br/> mytextureinfo = Texture2D.GetTextureInformation(TextureName);<br/> mytexture = Texture2D.FromFile(Device, TextureName);<br/> screenheight = Device.Viewport.Height;<br/> screenwidth = Device.Viewport.Width;<br/> tw = mytextureinfo.Width;<br/> th = mytextureinfo.Height / 2 - (screenheight/12);<br/> Reset();<br/> }<br/><br/> public void Reset()<br/> {<br/> x = (tw / 2) - (screenwidth / 2);<br/> y = th - screenheight ; <br/> frame.X = x;<br/> frame.Y = y;<br/> frame.Width = screenwidth;<br/> frame.Height = screenheight;<br/><br/> frame2.X = x;<br/> frame2.Y = y;<br/> frame2.Width = screenwidth;<br/> frame2.Height = screenheight;<br/><br/> }<br/> // ScrollingBackground.Update<br/> public void Update()<br/> {<br/> y -= speed;<br/> if (y < screenheight * -1)<br/> {<br/> Reset();<br/> return;<br/> }<br/> frame.Y = y;<br/> if (y < 0)<br/> {<br/> frame2.Y = th + y;<br/> frame2.Height = y * -1;<br/> }<br/><br/> <br/> }<br/> // ScrollingBackground.Draw<br/> public void Draw(SpriteBatch Batch)<br/> { <br/> // Draw the first texture<br/> Batch.Draw(this.mytexture, Pos, this.frame, Color.White);<br/> if (y < 0)<br/> {<br/> //draw second texture<br/> Batch.Draw(this.mytexture, Pos, frame2, Color.White);<br/> }<br/> }<br/> }</span>
页:
[1]