C# 从另一个class.XNA加载纹理

C# 从另一个class.XNA加载纹理,c#,xna,C#,Xna,我有以下问题: 我想从我的玩家类加载我的纹理。 因此,我将在我的player类中执行以下操作: public void Load(ContentManager Content) { Content.Load<Texture2D>("Images/pong"); } 但它说,我必须先使用new关键字,然后才能使用方法(我明白这一点)。如何修复该问题,并从其他类中正确加载纹理?只需交换这两条指令并将纹理保存到某个位置(您正在加载纹理,但未指定任何变

我有以下问题: 我想从我的玩家类加载我的纹理。 因此,我将在我的player类中执行以下操作:

   public void Load(ContentManager Content)
    {
        Content.Load<Texture2D>("Images/pong");
    }

但它说,我必须先使用new关键字,然后才能使用方法(我明白这一点)。如何修复该问题,并从其他类中正确加载纹理?

只需交换这两条指令并将纹理保存到某个位置(您正在加载纹理,但未指定任何变量):

MyPlayer=新玩家(新矢量2(500700),蝙蝠,新矢量2(5,5),新矢量2(蝙蝠宽度/2,蝙蝠高度/2),图形);
playerTexture=MyPlayer.Load(内容);
...
公共纹理2D加载(ContentManager内容)
{
返回内容。加载(“图像/乒乓”);
}
什么是“蝙蝠”?另外,您必须首先调用MyPlayer=newplayer(…),然后调用MyPlayer.Load()

我建议你这样做:

MyPlayer = new Player(POSITION, Content.Load<Texture2D>("PathWhereBatIs"), new Vector2(5,5),graphics);

我真的不明白你在说什么,介意给我看一个代码示例吗?听着,“蝙蝠”应该是我的纹理。当我试图编译代码时,它会抛出一个例外:您需要使用“new”关键字。。。如果playertexture不在构造函数中(这意味着我不能在我的类中将它用作纹理),那么制作它有什么意义呢@Joe:这让我觉得你实际上不知道构造函数的目的是什么。构造函数的目的是初始化类的字段,不是吗?@Joe:是的,但它也执行一些其他代码,除了您编写的代码之外,它实际上创建了类。这就是为什么你必须先打电话给它
MyPlayer = new Player(new Vector2(500, 700), Bat,new Vector2(5,5),new Vector2(Bat.Width / 2,Bat.Height/2),graphics);
playerTexture = MyPlayer.Load(Content);

...

public Texture2D Load(ContentManager Content)
{
    return Content.Load<Texture2D>("Images/pong");
}
MyPlayer = new Player(POSITION, Content.Load<Texture2D>("PathWhereBatIs"), new Vector2(5,5),graphics);
public Player(Vector pos, Texture2D tex, Vector2 ??, GraphicsDevice device)
{
    Vector2 Origin = new Vector2(tex.Width / 2f, tex.Height / 2f);

    ...
}