【发布时间】:2010-11-22 22:50:27
【问题描述】:
我正在为我的学校作业编写一个小游戏,该游戏是一个简单的 2D 游戏,包含怪物、物品和子弹。基本上你跑来跑去并试图收集所有物品硬币,怪物试图阻止你,你可以用你收集的子弹击落它们。很简单。
问题是,我已将怪物、物品、墙壁、玩家和子弹添加到名为 LiveObjects 的静态类中,然后我可以从代码中的任何位置访问这些对象。这是一个不好的做法吗?有什么替代方案? (不是多线程的)
LiveObjects.cs
internal static class LiveObjects
{
public static List<Item> items = new List<Item>(); // List with all the items
public static List<Monster> monsters = new List<Monster>(); // List with all present monsters
public static List<Wall> walls = new List<Wall>(); // List with the walls
public static List<Bullet> bullets = new List<Bullet>(); // List with the bullets
public static Player player = new Player(0, 0); // The player object
}
我使用很多不同的类来操作 LiveObjects 中的数据,然后为了避免传递整个 List,我可以直接在任何方法中调用它。
【问题讨论】:
-
根据下面大多数积极的反馈,这是一个很好的解决方案 Patrick,但我会用属性包装你的公共成员,而不是直接公开它们。使用属性将为您提供一些灵活性,并使您能够轻松地使用线程同步来访问您的对象。关于属性的简单教程:msdn.microsoft.com/en-us/library/aa288470(VS.71).aspx