C# Xna-共享相同纹理的对象

C# Xna-共享相同纹理的对象,c#,inheritance,xna,C#,Inheritance,Xna,在我的游戏中,我有一个Ai类,它基本上只是我游戏中每个Ai的链接列表。这个类保存每个ai的默认纹理,我的ai的所有独立类都继承自这个类,这样它们都可以继承ai类已经加载的默认纹理。然而,我似乎对此有问题。我的游戏在运行时从不加载gui,通过调试,看起来游戏的纹理有问题。是否无法加载单个纹理并将该纹理传递给另一个对象以供使用 人工智能等级: class AIs { private GraphicsDevice graphics; private ContentManager con

在我的游戏中,我有一个Ai类,它基本上只是我游戏中每个Ai的链接列表。这个类保存每个ai的默认纹理,我的ai的所有独立类都继承自这个类,这样它们都可以继承ai类已经加载的默认纹理。然而,我似乎对此有问题。我的游戏在运行时从不加载gui,通过调试,看起来游戏的纹理有问题。是否无法加载单个纹理并将该纹理传递给另一个对象以供使用

人工智能等级:

class AIs
{
    private GraphicsDevice graphics;
    private ContentManager content;
    private SpriteBatch spriteBatch;
    private LinkedList<object> ais;
    private LinkNode<object> current;

    //Default Textures
    private Texture2D robotTexture

    // Default Color Datas
    private Color[] robotColorData;

    public AIs()
    {
    }

    public void Load(ContentManager content, GraphicsDevice graphics, SpriteBatch spriteBatch)
    {
        this.spriteBatch = spriteBatch;
        this.graphics = graphics;
        this.content = content;

        // Loading Default Textures
        robotTexture = content.Load<Texture2D>("robot");

        // Loading Default Color Data
        robotColorData = new Color[robotTexture.Width * robotTexture.Height];
        robotTexture.GetData(robotColorData);
    }

    public void Update()
    {
        current = ais.getHead();

        while (current.getNext() != null)
        {
            if (current.getData() is Robot)
            {
                ((Robot)current.getData()).Update();
            }
        }
    }

    public void Draw()
    {
        current = ais.getHead();

        while (current.getNext() != null)
        {
            if (current.getData() is Robot)
            {
                ((Robot)current.getData()).Draw();
            }
        } 
    }

    public addRobot(float spawnX, float spawnY)
    {
        Robot temp = new Robot(spawnX, spawnY);
        temp.Load(content, graphics, spriteBatch);
        ais.add(temp);
    }

    public Texture2D getRobotTexture()
    {
        return robotTexture;
    }

    public Color[] getRobotColorData()
    {
        return robotColorData;
    }
}
//Default Textures
private static Texture2D robotTexture

// Default Color Datas
private static Color[] robotColorData;

事实证明,您所要做的就是在纹理和颜色数据旁边添加关键字static。 如果不放置static,那么在创建类时,它将继承方法和变量,但变量将是新的null,因为它是类的新实例。因此,将static放在它旁边可以使所有实例的值保持不变

在AI类中:

//Default Textures
private static Texture2D robotTexture

// Default Color Datas
private static Color[] robotColorData;

这里的问题似乎在于你对继承的使用

发生的事情是运行robots加载方法,该方法通过AIs getRobotTexture方法从自身获取robotTexture。该方法只返回robotTexture,因此您不妨编写robotTexture=robotTexture

但由于此实例尚未运行AIs.Load,robotTexture为null

残忍地诚实;阅读有关继承的文章

更乐于助人

看起来你真正想要的是一个机器人管理器,它可以简化机器人的繁殖。对于这一点,继承不是答案。请尝试以下方法:

public class RobotManager
{
    private SpriteBatch spriteBatch;
    private Texture2D robotTexture;

    private List<Robot> robots;

    public RobotManager(SpriteBatch spriteBatch, Texture2D texture)
    {
        this.spriteBatch = spriteBatch;
        this.robotTexture = texture;

        robots = new List<Robot>();
    }

    public void Update()
    {
        foreach (var robot in robots)
            robot.Update();
    }

    public void Draw()
    {
        foreach (var robot in robots)
            robot.Draw();
    }

    public void AddRobot(Vector2 position, Texture2D customTexture = null)
    {
        //Creates a new robot with position set and custom texture if specified
        var newRobot = new Robot(spriteBatch, position, (customTexture == null) ? robotTexture : customTexture);
        robots.Add(newRobot);
    }

}

是的,可以在多个对象上使用相同的纹理。你能发布一些与你的问题相关的代码吗?它将提供更多的洞察力,以便人们帮助你。我会的,但我有点担心,我应该显示什么代码。我会做一些接近它的东西,我想,我会在完成后把它挂起来。好的,在那里。我制作了一个代码副本,只显示了必要的代码。如果有什么不对劲,或者您需要更多,请告诉我。