从另一个类xna加载纹理



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

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

我将在我的主类中做这个;

MyPlayer.Load(Content);
        MyPlayer = new Player(new Vector2(500, 700), Bat,new Vector2(5,5),new Vector2(Bat.Width / 2,Bat.Height/2),graphics);

但是它说我必须在使用方法之前使用new关键字(我理解这一点)。我能做些什么来解决这个问题,并从另一个类正确加载纹理?

简单地交换两个指令并将纹理保存在某个地方(你正在加载它,但不分配给任何变量):

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");
}

什么是"Bat" ?另外,你必须先调用MyPlayer = new Player(…),然后调用MyPlayer. load()。

我建议这样做:

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

,然后在Player构造函数中获取蝙蝠纹理的起源,这样做:

public Player(Vector pos, Texture2D tex, Vector2 ??, GraphicsDevice device)
{
    Vector2 Origin = new Vector2(tex.Width / 2f, tex.Height / 2f);
    ...
}

最新更新