2013-05-26 3 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

밝혀, 당신이해야 할 키워드 옆에있는 질감과 색상 데이터를 "정적"을 추가합니다. static을 넣지 않으면 클래스가 생성 될 때 메서드와 변수를 상속 받지만 클래스의 새 인스턴스이기 때문에 변수는 null이됩니다. 따라서 정적을 옆에두면 모든 인스턴스에 대해 값이 동일하게 유지됩니다. 인공 지능 클래스에서

: 그것은 여기에 문제를 보인다

//Default Textures 
private static Texture2D robotTexture 

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

상속의 사용에있다.

로봇의 Load 메소드를 실행하면 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); 
    } 

} 
관련 문제