1

In my game, I have an Ai class which is basically just a linked list of every ai in my game. this same class holds the default textures for every ai, and all of my ai's seperate classes inherit from this class, that way they all can inherit the default textures that were already loaded by the ai class. However, I seem to be having problems with this. My game never loads up the gui when ran, and through debugging, it seems like the game has problems with the textures I am passing. Are you not able to load a single texture and pass that same texture for another object to use?

AI class:

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

Robot Class:

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

2 回答 2

0

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

在 AI 类中:

//Default Textures
private static Texture2D robotTexture

// Default Color Datas
private static Color[] robotColorData;
于 2013-05-27T09:21:22.427 回答
0

看来这里的问题在于您对继承的使用。

发生的情况是你运行你的机器人加载方法,它通过 AI 的 getRobotTexture 方法从自身获取robotTexture。该方法只返回robotTexture,所以你不妨写robotTexture = robotsTexture。

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

说实话;阅读继承!

更有帮助;

看来您真正追求的是拥有一个简化机器人生成的机器人管理器。为此,继承不是答案。尝试这样的事情:

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

}
于 2013-05-27T09:32:33.013 回答