C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 绘制“地板”纹理大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,让我说我有一些地板或其他东西的瓷砖纹理.而且我希望我的玩家能够继续前行.
如何设置此瓷砖使其成为@L_197_1@地板?
我需要这个瓷砖纹理遍布整个屏幕宽度吗?
我怎么做的?
谢谢

解决方法

如果你想要@L_197_1@非常简单的方法,这里是:
首先,您创建@L_197_1@新类并将其命名为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()中的tile纹理的纹理和位置,
我不知道你如何使用你的ContentManager,但这是@L_197_1@简单的方法

public void Initialize(ContentManager Content)
{
    texture = Content.Load<Texture2D>("YourfloorTexture"); //it will load your texture.
    position = new Vector2(); //the position will be (0,0)
}

现在我们需要多次绘制纹理,我们将如何做到这一点? thasc说的方式,代码可能更复杂,但这是你会理解的,我会添加@L_197_1@SpriteBatch,@R_401_9447@绘制.所有这些都是在public void Draw()中完成的:

public void Draw(SpriteBatch spriteBatch)
    {
        for (int i=0; i<30;i++) //will do a loop 30 times. Each Time i will = 
                                //a valor from 0 to 30.
        {
            spriteBatch.Draw(texture,position,Color.WhitE);
            //Will draw the texture once,at the position Vector2 
            //right Now position = (0,0)

            spriteBatch.Draw(texture,new Vector2((int)i,(int)i),Color.WhitE);
            //Will Draw the texture 30 times,the first time on the position (0,0)
            //Second Time on (1,1) .. third (2,2) etc...

            spriteBatch.Draw(texture,new Vector2((int)position.X + (i * texture.Width),(int)position.Y + (i * texture.Height),Color.WhitE));
            //Will Draw the Texture 30 times Spaced by the Width and height
            //of the texture (this is the code you need) 
        }

    }

我没试过但它应该可以工作,现在它只是@L_197_1@样本,你可以弄清楚剩下的.还有很多其他方法可以做到,但这个方法非常简单.好的,现在最后一步是实现这个类,所以进入你的主要类,你有你所有的代码,在此之前

public Game1()

创建tile类的新实例

Tile tile;

并在受保护的覆盖中初始化void Initialize():

tile = new Tile();
tile.Initialize(Content);

现在你必须在屏幕上绘制它,在类的末尾找到protected override void Draw(GameTime gameTimE)调用我们类的draw方法

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

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

大佬总结

以上是大佬教程为你收集整理的c# – 绘制“地板”纹理全部内容,希望文章能够帮你解决c# – 绘制“地板”纹理所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。