2013-03-01 150 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

相关问题