Xna -对象共享相同的纹理
本文关键字:纹理 共享 对象 Xna | 更新日期: 2023-09-27 18:15:20
在我的游戏中,我有一个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;
}
}
机器人类:
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);
}
}
结果是,你所要做的就是在纹理和颜色数据旁边添加关键字"static"。如果不放置static,那么在创建类时,它将继承方法和变量,但变量将是new null,因为它是类的新实例。因此,在它旁边放置static会使所有实例的值保持不变。
在AI类中://Default Textures
private static Texture2D robotTexture
// Default Color Datas
private static Color[] robotColorData;
问题似乎出在你对继承的使用上。
发生的是你运行你的机器人load方法,它通过它的getrobotture方法从自身获取robotexture。这个方法只返回robotTexture,所以你不妨写robotTexture = robotTexture。
但是由于这个实例没有运行ai。加载,robotexture为空
说实话;仔细研究一下继承!
更有帮助;
似乎你真正想要的是有一个机器人管理器,简化了机器人的产卵。对于这个问题,继承不是答案。试试这样做:
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);
}
}