【发布时间】:2012-06-15 10:17:49
【问题描述】:
最近我开始将 XNA 与 MonoMac 一起使用。 我上课有问题。 我想创建一个里面有纹理和位置信息的类。我也想做绘图功能。
我的想法是传递 spriteBatch 和 Content arround,这样我就可以加载纹理并在之后绘制它们。然而,我传递给这个对象的内容对象不加载任何纹理,当我在类之外尝试内容时,它加载的纹理很好,所以纹理必须在那里。
public class TPlayer {
public Texture2D[] textures;
public ContentManager Content;
public SpriteBatch spriteBatch;
public int currentFrame;
public Rectangle position;
public TPlayer(ContentManager ccontent, SpriteBatch cspritebatch){
this.Content = ccontent;
this.spriteBatch = cspritebatch;
this.Content.RootDirectory = "Content";
this.currentFrame = 0;
this.position = new Rectangle(250,20,100,150);
this.LoadContent();
}
protected void LoadContent(){
this.textures[0] = this.Content.Load<Texture2D>("Textures/ToughStanding");
this.textures[1] = this.Content.Load<Texture2D>("Textures/ToughWalking1");
this.textures[2] = this.Content.Load<Texture2D>("Textures/ToughWalking2");
}
public void Draw(){
spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.AlphaBlend);
this.spriteBatch.Draw (textures[0], this.position, Color.White);
this.spriteBatch.End ();
}
这就是我创建实例的方式:
Player = new TPlayer(this.Content,this.spriteBatch);
也许我试图使用错误的模型。也许我不应该在类中使用 spritebatch 和 Content,但是我可以让 spritebatch 和 content 全局化吗?
感谢您的帮助
【问题讨论】:
-
Content.RootDirectory 在进入你的 TPlayer 构造函数之前的值是多少?在构造函数中设置值,确定需要这样做吗?
-
另外作为提示(与您的问题无关)我猜您有一个吸引玩家的循环,调用 spritebatch.begin 并结束该循环的任一侧,而不是在每个绘制方法中,你会加快速度。
-
不,我认为我不需要这样做。之前设置为“内容”。我只是想猜测可能是哪里出了问题,所以我也试过了。感谢您对 spritebatch 的建议...从现在开始我只会调用 Begin 和 End 一次。
-
我通过只将纹理传递给对象而不是将 spritebatch 传递给绘图函数来解决问题,因为它在这里完成:create.msdn.com/en-US/education/tutorial/2dgame/… 抱歉给您带来麻烦
标签: c# xna xna-4.0 spritebatch