我想知道是否可以将对象与其实例名称匹配。
我得到了:
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
}
Orientation 是一个枚举,Animation 是一个类。
我可以使用相同的名称从 Orientation 调用正确的动画吗?
最佳答案
与其将动画存储在属性中,不如使用字典怎么样?
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]
访问适当的动画。
关于c# - 匹配 2 个具有相同实例名称的不同对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148204/