Building a 2D XNA Game Engine

I'd like to build an engine in XNA, primarily for a 2D RPG. At first, I began abstracting out some classes and built a Sprite class which wrapped a Vector2 and a Texture2D, and tried loading in content from the Sprite class' constructor. However, this didn't work. Now, it appears that I have to load all of the sprites within the Game class' LoadContent method. Is there anyway around this? I'd like to separate the actual Game Content from the inner workings of the engine, if possible.


Yes, you can pass the ContentManager as a parameter to your constructor so you can use this in your class. eg

public class MyClass
{
    ContentManager content;
    Texture2D sprite;

    public MyClass(ContentManager content)
    {
        this.content = content;
    }

    public void LoadSprite(string filename)
    {
        sprite = this.content.Load<Texture2D>(filename);
    }
}

public class Game1
{
    ContentManager content;

    public void LoadContent()
    {
         MyClass myclass = new MyClass(content);
    }

}
链接地址: http://www.djcxy.com/p/95514.html

上一篇: XNA:将对象序列化到独立存储

下一篇: 构建2D XNA游戏引擎