c# – 绘制“地板”纹理
好吧,让我说我有一些地板或其他东西的瓷砖纹理.而且我希望我的玩家能够继续前行.
如何设置此瓷砖使其成为一个地板? 我需要这个瓷砖纹理遍布整个屏幕宽度吗? 我怎么做的? 谢谢 解决方法
如果你想要一个非常简单的方法,这里是:
首先,您创建一个新类并将其命名为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,现在我们将初始化我们的 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说的方式,代码可能更复杂,但这是你会理解的,我会添加一个SpriteBatch,所以我可以绘制.所有这些都是在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) } } 我没试过但它应该可以工作,现在它只是一个样本,你可以弄清楚剩下的.还有很多其他方法可以做到,但这个方法非常简单.好的,现在最后一步是实现这个类,所以进入你的主要类,你有你所有的代码,在此之前: 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(); 这是完成简单的简单平铺系统的所有步骤.正如我所说,还有很多其他方法你只需阅读有关它们的教程或自己创建它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |