C# 图纸a";“地板”;纹理

C# 图纸a";“地板”;纹理,c#,xna,C#,Xna,好吧,假设我有一个地板或其他东西的瓷砖纹理。我希望我的球员能在上面行走。 我如何将这块瓷砖设置为地板? 我需要整个屏幕都有瓷砖纹理,对吗? 我是怎么做到的? 谢谢如果您想要一种真正简单的方法,请点击这里: 首先创建一个新类并将其命名为Tile: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; // Don't f

好吧,假设我有一个地板或其他东西的瓷砖纹理。我希望我的球员能在上面行走。 我如何将这块瓷砖设置为地板? 我需要整个屏幕都有瓷砖纹理,对吗? 我是怎么做到的?
谢谢

如果您想要一种真正简单的方法,请点击这里: 首先创建一个新类并将其命名为Tile:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework; // Don't forget those, they will let you
using Microsoft.Xna.Framework.Content; // access some class like:
using Microsoft.Xna.Framework.Graphics; // Texture2D or Vector2

namespace Your_Project _Name
{
    class Tile
    {
    }
{
到目前为止还不错,现在在类中创建纹理和位置,如下所示:

namespace Your_Project _Name
{
    class Tile
    {
        Texture2D texture;
        Vector2 position;

        public void Initialize()
        {
        }

        public void Draw()
        {
        }
    }
{
如您所见,我还创建了两个方法,Initialize和Draw,现在我们将初始化 公共void Initialize()中平铺纹理的纹理和位置, 我不知道如何使用ContentManager,但这里有一个简单的方法:

public void Initialize(ContentManager Content)
{
    texture = Content.Load<Texture2D>("YourfloorTexture"); //it will load your texture.
    position = new Vector2(); //the position will be (0,0)
}
创建平铺类的新实例

Tile tile;
并在受保护的覆盖void Initialize()中对其进行初始化:

现在您必须在屏幕上绘制它,在类的末尾找到受保护的覆盖无效绘制(GameTime GameTime),并调用我们类的绘制方法:

spriteBatch.Begin();
tile.Draw(spriteBatch);
spriteBatch.End();

这是完成简单平铺系统的所有步骤。正如我所说,还有很多其他方法,您只需阅读有关它们的教程或自己创建它们。

如果您不打算对平铺背景做任何额外的工作,我建议您使用thasc的解决方案和

为此,创建一个与背景一样大的矩形,并将
samplestate.LinearWrap
传递给
SpriteBatch.Begin
,然后在背景矩形上调用
Draw

Rectangle backgroundRect = new Rectangle(0, 0, backWidth, backHeight);

spriteBatch.Begin(..., ..., SamplerState.LinearWrap, ..., ...);
spriteBatch.Draw(backgroundTexture, backgroundRect, Color.White);
spriteBatch.End();
如果你好奇的话,这会创建一个覆盖背景区域的多边形,它会从0.0f到
背景宽度
抓取纹理的坐标。纹理通常在(0.0f,0.0f)和(1.0f,1.0f)之间映射,表示给定纹理的角点。如果超出这些边界,
TextureAddressMode
将定义如何处理这些坐标:

  • 夹紧
    将坐标削减回0-1范围
  • Wrap
    将坐标包装回0,因此0.0=2.0=4.0=等,1.0=3.0=5.0=等
  • Mirror
    也将进行包裹,但在渲染多边形时,每隔一次会镜像纹理,基本上是从左到右到左等

for(int x=0;x)顺便说一句,它不会编译,但这就是它的想法。似乎有一种更受支持的方法,可以通过谷歌搜索“xna tiling”在两秒钟内找到。@Thasc,我建议添加这一点作为答案,当然还有一些关于实际使用的详细信息。
tile = new Tile();
tile.Initialize(Content);
spriteBatch.Begin();
tile.Draw(spriteBatch);
spriteBatch.End();
Rectangle backgroundRect = new Rectangle(0, 0, backWidth, backHeight);

spriteBatch.Begin(..., ..., SamplerState.LinearWrap, ..., ...);
spriteBatch.Draw(backgroundTexture, backgroundRect, Color.White);
spriteBatch.End();