开发者

A question about scrolling backgrounds in XNA

开发者 https://www.devze.com 2023-03-10 01:01 出处:网络
I\'m working on scrolling backgrounds for my game and I\'m kind of stumped on how I should repeat the textur开发者_如何转开发e. Thus far I have it so that the background class takes five variables: tw

I'm working on scrolling backgrounds for my game and I'm kind of stumped on how I should repeat the textur开发者_如何转开发e. Thus far I have it so that the background class takes five variables: two floats (one for each axis, both clamped to 0.01 and 1.0), two bools (to see if it should repeat on either axis) and the camera. It draws the texture to a rectangle and the rectangle's position moves with the camera's position multiplied by the appropriate float.

The movement code thus far is just:

source.X = (int)(camera.Pos.X * scrollX);
source.Y = (int)(camera.Pos.Y * scrollY);

So what I'd like to do is make it so that the texture repeats on the appropriate axis (or axes) when one or both of the repeat bools is true, but I have no idea how I can do it with my current system. I was thinking I could add three or more rectangles (I usually make my backgrounds pretty big), have them all draw the same texture and move around according to where the camera is, but I'm not exactly sure how I could do that. Any suggestions?


I haven't tested this, but I'm pretty sure you should be able to use the overload of SpriteBatch.Draw that takes both a source and a destination rectangle. Set your destination rectangle to cover your screen. Set your source rectangle to the negation of your background's position (or something like that; it will go beyond the edges of your texture).

You will probably have to set the SamplerState for the sprite batch to one of the sampler states that wraps (eg LinearWrap).

0

精彩评论

暂无评论...
验证码 换一张
取 消