2013-10-30 3 views
0

애니메이션 스프라이트 시트를 가져 오기 위해 AnimatedTextureData 클래스를 설정하기 위해 내 XNA 게임에서 플레이어를 움직이게하려고합니다. 이것은 일반 textureData를 상속합니다.애니메이션 스프라이트 만들기 시도

AnimatedTextureData

namespace GDLibrary 
{ 
    public class AnimatedTextureData : TextureData 
    { 
     //width and height of a single frame inside the animation 
     private int frameWidth, frameHeight, numberOfFrames; 

     //this is a list containing all the source rectangle color data 
     protected List<Color[,]> sourceColorDataList; 

     public Color[,] this[int index] 
     { 
      get 
      { 
       return sourceColorDataList[index]; 
      } 
     } 

     public int FRAMECOUNT 
     { 
      get 
      { 
       return numberOfFrames; 
      } 
     } 
     public int FRAMEWIDTH 
     { 
      get 
      { 
       return frameWidth; 
      } 
     } 
     public int FRAMEHEIGHT 
     { 
      get 
      { 
       return frameHeight; 
      } 
     } 

     public AnimatedTextureData(Main game, string path, int numberOfFrames, int frameWidth, int frameHeight) 
      : base() 
     { 
      this.texture = game.Content.Load<Texture2D>(@"" + path); 

      this.numberOfFrames = numberOfFrames; 
      this.frameWidth = frameWidth; 
      this.frameHeight = frameHeight; 

      this.sourceColorDataList = new List<Color[,]>(numberOfFrames); 
      setColorData(texture); 
     } 

     /// <summary> 
     /// Converts a Texture2D into a list of Color[,] array data 
     /// e.g. an image with 8 frames will have 8 Color[,] entries in the list. 
     /// Each Color[,] is a 2D array of color data for the frame. 
     /// This 2D color array is used for Non-AA CDCR - see Collision class 
     /// </summary> 
     /// <param name="texture"></param> 

     protected override void setColorData(Texture2D texture) 
     { 
      int width = texture.Width; 
      int height = texture.Height; 

      //read data into 1d array 
      Color[] colors1D = new Color[width * height]; 
      texture.GetData(colors1D); 

      //create 2d array to store data 
      Color[,] colors2D = new Color[frameWidth, frameHeight]; 

      //read each frame into a seperate colors2D array and add it to the list 
      //then when we want to now the color data for a particular frame we just query the list 
      for (int i = 0; i < numberOfFrames; i++) 
      { 
       for (int x = 0; x < frameWidth; x++) 
       { 
        for (int y = 0; y < frameHeight; y++) 
        { 
         colors2D[x, y] = colors1D[x + (y * frameWidth) + frameWidth * frameHeight * i]; 
        } 
       } 
       sourceColorDataList.Add(colors2D); 
      } 
     } 
    } 
} 

textureData

private Vector2 centreOrigin; 
     private int halfWidth; 
     private int halfHeight; 
     private Integer2 dimensions; 

     #region PROPERTIES 
     public Integer2 DIMENSIONS 
     { 
      get 
      { 
       return dimensions; 
      } 
      set 
      { 
       dimensions = value; 
      } 
     } 

     public float WIDTH 
     { 
      get 
      { 
       return texture.Width; 
      } 
     } 
     public float HALFWIDTH 
     { 
      get 
      { 
       return halfWidth; 
      } 
     } 
     public float HEIGHT 
     { 
      get 
      { 
       return texture.Height; 
      } 
     } 
     public float HALFHEIGHT 
     { 
      get 
      { 
       return halfHeight; 
      } 
     } 
     public Color[,] TEXTURECOLORDATA2D 
     { 
      get 
      { 
       return textureColorData2D; 
      } 
      set 
      { 
       textureColorData2D = value; 
      } 
     } 
     public Texture2D TEXTURE 
     { 
      get 
      { 
       return texture; 
      } 
      set 
      { 
       texture = value; 
      } 
     } 
     public string NAME 
     { 
      get 
      { 
       return texture.Name; 
      } 
     } 
     public Vector2 CENTREORIGIN 
     { 
      get 
      { 
       return centreOrigin; 
      } 
     } 
     public Rectangle FULLSOURCERECTANGLE 
     { 
      get 
      { 
       return fullSourceRectangle; 
      } 
     } 
     #endregion 

     //Called by AnimatedTextureData - does nothing because AnimatedTextureData() does everything instead 
     public TextureData() 
     { 
     } 

     public TextureData(Main game, string path) 
     { 
      this.texture = game.Content.Load<Texture2D>(@"" + path); 
      setColorData(texture); 

      this.fullSourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height); 
      this.centreOrigin = new Vector2(texture.Width/2, texture.Height/2); 
      this.halfWidth = texture.Width/2; 
      this.halfHeight = texture.Height/2; 
      this.dimensions = new Integer2(texture.Width, texture.Height); 
     } 

     //converts color data from texture from 1d to 2d array 
     protected virtual void setColorData(Texture2D texture) 
     { 
      System.Diagnostics.Debug.WriteLine("once"); 

      int width = texture.Width; 
      int height = texture.Height; 

      //read data into 1d array 
      Color[] colors1D = new Color[width * height]; 
      texture.GetData(colors1D); 

      //create 2d array to store data 
      this.textureColorData2D = new Color[width, height]; 

      for (int x = 0; x < width; x++) 
      { 
       for (int y = 0; y < height; y++) 
       { 
        textureColorData2D[x, y] = colors1D[x + y * width]; 
       } 
      } 
     } 
    } 
} 

텍스처 관리자는 데이터를 제어하고 메인에서 이러한 클래스를 사용하는 경우 당신은 내가 여기 데 문제 textureManager.Add("Example"); 전화 내가 캐스팅을 원해서 AnimatedTextureData에 던지려고하지만 그것은 나를 허용하지 않을 것입니다. 어떤 아이디어?

AnimatedTextureData aniSpri = (AnimatedTextureData)textureManager.Get("AniSpri"); 

내가 문제를 얻는 경우에

+0

Texture Manager를 설명하는 코드 또는'textureManager.Get()'이 보이지 않습니다. – pinckerman

답변

0

textureManager.Get()TextureData를 반환 (AniSpri 이미 TextureManager에 사전에 넣고).

하위 유형을 기본 유형으로 변환 할 수 있습니다. 하지만 하위 유형에 기본 유형의 인스턴스를 캐스팅하고 있습니다. 그렇게 할 수 없습니다.

AnimatedTextureData

TextureData이지만, TextureDataAnimatedTextureData의 특별한 종류가 아닙니다.

참고 here.

관련 문제