iPhone 游戏设计中的单例/全局变量
我正在创建一款 iPhone 游戏,其中角色在不同视图之间移动,因此每个视图本身本质上都是一个新游戏。但现在我想创造生命值,所以如果生命值在一个视图中下降到 5 hp,那么当他们更改为不同的视图时,生命值仍将是 5。我做了一些研究,听起来我需要的是单例或全局变量。但我所看到的一切对我来说毫无意义。我的两个应用程序委托中都没有任何内容,并且我没有使用任何框架,例如 cocos2d 或 OpenGLES。发布代码现在似乎不起作用,所以如果您能告诉我您需要查看哪些代码来帮助您回答我的问题。
I'm creating an iPhone game where the character moves between different views, so each view is essentially a new game in itself. But now I want to create health, so if health goes down in one view to say 5 hp, then when they change to a different view the health will still be 5. I've done some research and it sounds like what I need is a singleton or a global variable. But everything I've seen makes no sense to me. I have nothing in either of my app delegates, and I'm not using any frameworks like cocos2d or OpenGLES. Posting the code doesn't seem to be working right now, so if you could tell me what code you need to see to help you answer my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是在视图控制器之间共享角色数据。单例是共享数据的一种方式;它在概念上非常简单,但常常会带来不幸的后果。有关其中一些后果的讨论,请参阅单例有什么不好? 。有关其他一些方法,请参阅单例的替代方案。
如果您确实决定使用单例(不推荐),则在为该对象选择角色时应该小心。例如,您的第一个想法可能是创建一个代表玩家或角色的单例。然而,这意味着你的游戏中只能有一个玩家或角色。更好的选择是创建一个玩家管理器,您可以使用它来访问当前玩家。
更好的选择是放弃单例并为每个视图控制器提供
player
和gameController
属性(假设您有一些负责整个游戏的对象。游戏控制器将通过实例化相应的视图控制器、将gameController
属性设置为自身并将player
属性设置为当前玩家对象来启动每个新的“板”。视图控制器不必向外寻找当游戏的一部分完成时,它可以使用其gameController
属性向游戏控制器发送一条消息:“我完成了,您可以继续进行下一块板”或任何合适的内容。What you need is to share your character data between view controllers. A singleton is one way to share data; it's conceptually pretty simple but often comes with unfortunate consequences. See What is so bad about singletons? for a discussion of some of those consequences. See Alternatives to Singletons for some other approaches.
If you do decide to use a singleton (not recommended), you should be careful about choosing a role for that object. For example, your first thought might be to create a singleton that represents the player or character. However, that would mean that you could only ever have a single player or character in your game. A better choice would be to create a player manager, which you could use to access the current player.
A still better choice would be to forgo the singleton and give each view controller
player
andgameController
properties (assuming that you have some object that's in charge of the overall game. The game controller would start each new "board" by instantiating the corresponding view controller, setting thegameController
property to itself, and setting theplayer
property to the current player object. That way, the view controller doesn't have to look outside itself to find out about the player. When its part of the game is finished, it can use itsgameController
property to send a message to the game controller that says: "I'm done, you can move on to the next board" or whatever is appropriate.从技术上讲,可以从应用程序中的任何位置访问任何内容,因为我们可以像这样访问应用程序委托:
所以如果您像这样在应用程序委托中定义一个属性:
您可以像这样访问它......
Technically, anything can be access from anywhere in an app because we can access the app delegate like so:
so if you define a property in the app delegate like so:
you can access it like so...