2013-05-26 89 views
1

在我的遊戲中,我有一個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); 
    } 
} 
+0

是的,可以跨多個對象使用相同的紋理。你能發表一些與你的問題有關的代碼嗎?它將提供更多的洞察力,以便人們能夠幫助你。 – jgallant

+0

我會但我有點擔心,我應該顯示什麼代碼。 – user2375782

+0

我會做出接近它的東西我想,當我完成它時我會放下它。 – user2375782

回答

0

事實證明,所有你需要做的就是添加關鍵字 「靜態」 旁邊的質地和顏色數據。 如果不放置靜態,那麼當類創建時,它將繼承方法和變量,但變量將爲new,因爲它是該類的新實例,則爲new。因此,將靜態值放在它旁邊會使所有實例的值保持不變。

在AI類:

//Default Textures 
private static Texture2D robotTexture 

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

在這裏看來問題是在你使用繼承。

會發生什麼事是你運行你的機器人Load-method,它通過AIs getRobotTexture-method從自身獲取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); 
    } 

}