Looking for concept for managing game level views, level selection views, preferences view, storing levels, environment variables

六眼飞鱼酱① 提交于 2019-12-04 19:33:18

Typically my games have an App object at the top, which owns one of several AppStates (menu, selector, preferences, etc) and switches between them as required, in a pretty typical usage of the State pattern. These states handle their own rendering and input and store whatever resources they need. The App object also owns any global application-wide settings and objects that are shared across the states (eg. rendering, sound). These may be passed in to the states individually, or the states could request the relevant interfaces back from the App at some point.

One of the AppStates will be the game playing state, and that will contain the definition of the current puzzle, plus the current state of this play session (eg. how completed it is). I would tend to still have a separate Game class that is owned by the relevant GamePlayingState, since the former would contain only game logic information and the latter handles input/output.

I think I should use the NSNotification class. It is simply set up a "listener" in the object (viewController) that contains the subviews, then subviews can send notifications to the controller. Then the notification handler can invoke any methods.

viewController part:

-(void) viewDidLoad
{   
//Set up a listener.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"finishedCurrentLevel" object:nil];   
...
}

-(void) notificationHandler: (NSNotification*) notification
{
//Notification handling.
if ([notification name] == @"finishedCurrentLevel") [self finishedCurrentLevel];
}

-(void) finishedCurrentLevel
{
//View managing code here...
}

The notification, the listening and the "responds" for the notifications set up this was. The actual notifying goes like this (can be executed from any subviews):

[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedCurrentLevel" object:nil];

It solves my "communication" problem, I think.

About globals I simply created a separate globals.m file with the coressponding globals.h without defining any class. They just "attach" some extern variables, so I can reach them from any file having globals.h imported.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!