Loading textures from another class.XNA
I'm having the following issue: I want to load my texture from my player class. So I'll do the foll开发者_JAVA百科owing in my player class:
public void Load(ContentManager Content)
{
Content.Load<Texture2D>("Images/pong");
}
And I'll do this in my main class;
MyPlayer.Load(Content);
MyPlayer = new Player(new Vector2(500, 700), Bat,new Vector2(5,5),new Vector2(Bat.Width / 2,Bat.Height/2),graphics);
But it says that I have to use the new keyword before I can use methods(and I understand that). What can I do to fix that, and load textures properly from another classes?
Simply swap the 2 instructions and save the texture somewhere (you're loading it but not assigning to any variable):
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");
}
What "Bat" is? Also, you have to first call MyPlayer = new Player(...), and then call MyPlayer.Load().
I'd recommend doing something like this:
MyPlayer = new Player(POSITION, Content.Load<Texture2D>("PathWhereBatIs"), new Vector2(5,5),graphics);
and then in Player constructor to get origin of Bat-texture do this:
public Player(Vector pos, Texture2D tex, Vector2 ??, GraphicsDevice device)
{
Vector2 Origin = new Vector2(tex.Width / 2f, tex.Height / 2f);
...
}
精彩评论