在我的遊戲中,我有一個Ai類,它基本上只是我遊戲中每個ai的鏈接列表。這個類保存了每個ai的默認紋理,並且我所有的ai的單獨類都從這個類繼承,這樣它們都可以繼承已經由ai類加載的默認紋理。不過,我似乎遇到了這個問題。我的遊戲在運行時永遠不會加載gui,並且通過調試,看起來遊戲與我傳遞的紋理有關。您是否無法加載單個紋理並將另一個對象的相同紋理傳遞給使用?Xna - 共享相同紋理的物體
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);
}
}
是的,可以跨多個對象使用相同的紋理。你能發表一些與你的問題有關的代碼嗎?它將提供更多的洞察力,以便人們能夠幫助你。 – jgallant
我會但我有點擔心,我應該顯示什麼代碼。 – user2375782
我會做出接近它的東西我想,當我完成它時我會放下它。 – user2375782