开发者

HELP loading strings for levels of my iphone game in xcode

So im conceptually stuck building my app. I've read lots of forums but I think I need to try a new route. Heres what im trying to do.

I created a game where each level loads a different string for the user to play with. Right now the strings for the levels are stored in a plist. There are 5 categories (arrays) each of which contains 20 different strings (ie 20 levels) so there are 100 levels total.

When the user starts the game they reach the "LevelSelect" view which consists of a picker. The picker has two components. the first component is a list of categories, and the second component is the list of levels in that category. The user selects which of the levels they want to play, then press a "Start" button to bring up开发者_高级运维 the "GamePlay" view (an entirely different set of .h, .m, and .xib files).

MY PROBLEM: I dont know how to make the selected level appear on the screen in the Gameplay class. I have all the machinery of the game working, so if I create a string in the viewDidLoad, the game appears to work fine, but I want to have the string for that level come onto the screen when that level is selected and the user presses start.

MY QUESTION: How do I pass the string from LevelSelect to GamePlay?

Thanks for any help you can offer!


If I understand your question correctly, LevelSelect and GamePlay are both subclasses of UIViewController. The user chooses the level he wants to play through the LevelSelect view controller. It sounds like you already know how to display the view for the GamePlay view controller. You just don't know how to pass a string to that view controller before you display it.

When the user presses the start button, I presume that you're currently constructing and presenting your GamePlay view controller with code that looks something like this:

- (IBAction)startButtonPressed:(id)sender {
    GamePlay *gamePlayViewController = [[[GamePlay alloc] initWithNibName:nil bundle:nil] autorelease];
    [self presentModalViewController:gamePlayViewController animated:NO];
}

Your goal is give your GamePlay view controller a levelString property that you can set like this:

- (IBAction)startButtonPressed:(id)sender {
    GamePlay *gamePlayViewController = [[[GamePlay alloc] initWithNibName:nil bundle:nil] autorelease];
    gamePlay.levelString = self.selectedLevelString;
    [self presentModalViewController:gamePlayViewController animated:NO];
}

where self.selectedLevelString is the string for the level the user selected in the picker.

To achieve this you'll need to declare the property in your GamePlay.h file like this:

@interface GamePlay : UIViewController {
}
@property (nonatomic, retain) NSString *levelString;
@end

In your GamePlay.m file you'll need to synthesize this property and release it in your dealloc method like this:

@implementation GamePlay
@synthesize levelString;

- (void)dealloc {
    self.levelString = nil;
    [super dealloc];
}
@end

This gives your GamePlay controller a levelString property, which you can set from your LevelSelect view controller as shown above. Then within any method in the GamePlay view controller, you can access this string using self.levelString.

By the way, my example code for startButtonPressed: above presumes that you have a property on your LevelSelect view controller called selectedLevelString. You can declare this property the exact same way we declared the levelString property on GamePlay. You can set this string as appropriate when the user selects a level in pickerView:didSelectRow:inComponent:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜