简体   繁体   English

Xna-共享相同纹理的对象

[英]Xna - Objects sharing the same texture

In my game, I have an Ai class which is basically just a linked list of every ai in my game. 在我的游戏中,我有一个Ai类,它基本上只是我游戏中每个ai的链接列表。 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. 这个相同的类为每个ai保留默认纹理,而我的ai的所有单独类都继承自该类,因此它们都可以继承ai类已经加载的默认纹理。 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. 我的游戏在运行时从未加载过gui,并且通过调试,似乎游戏在传递的纹理方面存在问题。 Are you not able to load a single texture and pass that same texture for another object to use? 您是否无法加载单个纹理并将同一纹理传递给另一个对象使用?

AI class: 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;
    }
}

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

Turns out, all you have to do is add the keyword "static" next to the texture and color data. 事实证明,您要做的就是在纹理和颜色数据旁边添加关键字“ static”。 If you don't put static, then when the class is created, it will inherit the methods and variables, but the variables will be new, null, because it is a new instance of the class. 如果不放置静态变量,则在创建类时,它将继承方法和变量,但是变量将是新的,为null,因为它是类的新实例。 So putting static next to it makes the value stay the same for all instances. 因此,将static放在其旁边会使所有实例的值保持相同。

In the AI class: 在AI类中:

//Default Textures
private static Texture2D robotTexture

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

It seems the problem here is in your use of inheritance. 看来这里的问题在于您对继承的使用。

What happens is that you run your robots Load-method, which fetches robotTexture from itself through AIs getRobotTexture-method. 发生的是,您运行了机器人的加载方法,该方法通过AIsgetRobotTexture方法从自身获取robotTexture。 That method just returns robotTexture, so you might as well write robotTexture = robotTexture. 该方法仅返回robotTexture,因此您最好编写robotTexture = robotTexture。

But since this instance has not ran AIs.Load, robotTexture is null. 但是由于此实例尚未运行AIs.Load,所以robotTexture为null。

To be brutally honest; 说实话; Read up on inheritance! 阅读继承!

To be more helpful; 更有用;

It seems that what you are really after is having a robotmanager that simplifies the spawning of robots. 看来您真正想要的是拥有一个简化了机器人生成的机器人管理器。 For this, inheritance is not the answer. 为此,继承不是答案。 Try something like this instead: 尝试这样的事情:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM