Xna - 共享相同纹理的对象

时间:2013-05-26 19:35:48

标签: c# inheritance xna

在我的游戏中,我有一个Ai课程,它基本上只是我游戏中每个ai的链接列表。这个相同的类保存每个ai的默认纹理,并且我的所有ai的单独类都继承自这个类,这样它们都可以继承已经由ai类加载的默认纹理。但是,我似乎遇到了这个问题。我的游戏在跑步时从不加载gui,并且通过调试,似乎游戏在我传递的纹理方面存在问题。您是否无法加载单个纹理并将相同的纹理传递给另一个对象?

AI班:

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

机器人类:

class Robot : AIs
{
    private GraphicsDevice graphics;
    private ContentManager content;
    private SpriteBatch spriteBatch;
    private Texture2D robotTexture;
    private Color[] robotColorData;
    private Rectangle robotRectangle;
    private Vector2 robotPosition = Vector2.Zero;

    public Robot(float spawnX, float spawnY)
    {
        robotPosition = new Vector2(spawnX, spawnY);
    }

    new public void Load(ContentManager content, GraphicsDevice graphics, SpriteBatch spriteBatch)
    {
        this.spriteBatch = spriteBatch;
        this.graphics = graphics;
        this.content = content;
        robotTexture = getRobotTexture();
        robotColorData = getRobotColorData();
    }

    new public void Update()
    {
        robotRectangle = new Rectangle((int)robotPosition.X, (int)robotPosition.Y, robotTexture.Width, robotTexture.Height);

    }

    new public void Draw()
    {
        spriteBatch.Draw(robotTexture, robotPosition, Color.White);
    }
}

2 个答案:

答案 0 :(得分:0)

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

在AI课程中:

//Default Textures
private static Texture2D robotTexture

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

答案 1 :(得分:0)

这似乎是你使用继承的问题。

您运行的机器人Load-method会通过AI 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);
    }

}