我正在创建我的第一个游戏,目前我已经设置了一个“GameState”类,用于在内部存储玩家的健康状况等。该类当前是从 AppDelegate 实例化的,因为我需要从整个游戏中访问它。
这很好。对于我正在使用的每个类,我可以访问应用程序委托,然后找到 GameState 对象...但是,它看起来非常混乱。我很想找到一种方法(我仍然是新手)将 GameState 实例定义为某种全局变量,以便我可以轻松地从各处访问它......但是我对变量的一点阅读范围让我对这样做感到不安,即使我知道如何做。
关于定义和访问此类的最佳方式有什么想法吗?它将用于从玩家健康状况到他们发现的物品、任何个性化设置等所有内容。
谢谢!
I'm creating my first game, and I've currently set up a 'GameState' class, to store player health etc. inside. This class is currently instantiated from the AppDelegate as I need to access it from all over my game.
This is fine. For each class I'm working in, I can access the app delegate, and then find the GameState object... however, it seems very messy. I'm tempted to find a way (I'm still a newbie) to define the GameState instance as being some sort of global variable so that I can access it from all over with ease... but my little bit of reading on variable scope makes me uneasy about doing this, even if I knew how.
Any ideas of the best way to define and access this class? It'll be used for everything from player health, to items they've found, any personalisation etc.
Thanks!
发布评论
评论(2)
您也许可以使用 singleton。
You could perhaps use a singleton.
也许 关键-值观察或通知是更好的选择。
Perhaps Key-Value Observing or Notifications are a better choice.