2011-09-03 90 views
1

我遇到以下问题: 我想从我的玩家类加载我的纹理。 所以我会尽我的播放器类在以下几点:从另一个类加载纹理.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); 

但它说,我必须使用新的关键字,才可以使用方法(我明白这一点)。我能做些什么来解决这个问题,并从另一个类中正确加载纹理?

回答

0

只需简单更换了2个指令,并保存在某个地方纹理(你加载它而不是分配给任何变量):

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

我还没有真正理解你”请说,请介意给我一个代码示例吗? – Joe

+0

@Joe:这里是 – BlackBear

+0

听着,“蝙蝠”应该是我的质感。当我试图编译代码时,它会抛出一个异常,并说:你需要使用“new”关键字......以及如果构造函数中没有使用playertexture(这意味着我不能使用它作为我班的纹理?) – Joe

0

什么“蝙蝠”是什么?此外,您必须先调用MyPlayer = new Player(...),然后调用MyPlayer.Load()。

我建议你做这样的事情:

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

    ... 
}