2013-03-01 151 views
4

我想知道是否可以將對象與其實例名稱相匹配匹配2具有相同實例名稱的不同對象

我:

class AnimatedEntity : DrawableEntity 
{ 
    Animation BL { get; set; } 
    Animation BR { get; set; } 
    Animation TL { get; set; } 
    Animation TR { get; set; } 
    Animation T { get; set; } 
    Animation R { get; set; } 
    Animation L { get; set; } 
    Animation B { get; set; } 

    Orientation orientation ; 

    public virtual int Draw(SpriteBatch spriteBatch, GameTime gameTime) 
    { 
     //draw depends on orientation 
    } 
} 

enum Orientation { 
    SE, SO, NE, NO, 
    N , E, O, S, 
    BL, BR, TL, TR, 
    T, R, L, B 
} 

方向在哪裏是一個ENUM和動畫類。

我可以從同一個名字的方向調用正確的動畫嗎?

+0

是否有可能注入'Animation'到'Orientation'對象:

class AnimatedEntity : DrawableEntity { Dictionary<Orientation, Animation> Animations { get; set; } public AnimatedEntity() { Animations = new Dictionary<Orientation, Animation>(); } public Animation this[Orientation orientation] { get{ return Animations[orientation]; } set{ Animations[orientation] = value;} } Orientation Orientation { get; set; } public void Draw(SpriteBatch spriteBatch, GameTime gameTime) { Animation anim = Animations[Orientation]; } } 

會像使用嗎? – IAbstract 2013-03-01 00:21:38

+0

沒有動畫取決於一個實體實例,取向取決於現場 – 2013-03-01 00:22:51

+0

我覺得這可能是:typeof(MyClass).AssemblyQualifiedName,但它只是一半的方式 – 2013-03-01 00:23:02

回答

3

而不是將動畫存儲在屬性中,如何使用字典?

Dictionary<Orientation, Animation> anim = new Dictionary<Orientation, Animation> { 
    { Orientation.BL, blAnimation }, 
    { Orientation.BR, brAnimation }, 
    { Orientation.TL, tlAnimation }, 
    { Orientation.TR, trAnimation }, 
    { Orientation.T, tAnimation }, 
    { Orientation.R, rAnimation }, 
    { Orientation.L, lAnimation }, 
    { Orientation.B, bAnimation } 
}; 

然後,您可以使用anim[orientation]訪問相應的動畫。

+0

我怎麼沒有想到那個......上午2點......好吧我去睡覺,謝謝。 – 2013-03-01 00:44:42

1

事實上,一個Dictionary將是一個不錯的選擇。它甚至可以有一個Animation指數如果動畫會從外部設置:

AnimatedEntity entity = new AnimatedEntity(); 
entity[Orientation.B] = bAnimation; 
entity[Orientation.E] = eAnimation; 
entity[Orientation.SE] = seAnimation; 
+0

事實上,index的語法比'public void addAnimation(Orientation or,Animation an){anims.Add(or,an); }'。感謝您提供更多信息。 – 2013-03-01 07:15:35

相關問題