views:

241

answers:

1

Hi Devs,

I m developing a game where all the settings page, score page, help pages are in nib formace and the game is in cocos2d scene format (Gamescene.h and m file). So i need to call the game scene of from a nib file when "Start game" button is pressed from the nib. And when the game is over , i need to call the score .nib from the cocos scene. But i've no idea How to do that....

Can anyone plz give me a easy solution? Thanks in advance!

Regards, Benzamin.

+2  A: 

Hi,

In apps I've created I usually attach the cocos2d director to a view i.e.

[[Director shareddirector] attachInView:myView];

This means that I can overlay other views loaded from nibs in front of it. E.g. I could do something like . . .

in YourAppDelegate.h

GameViewController *gameViewController;
ScoresViewcontroller *scoresViewController;

....

@property (nonatomic, retain) GameViewController *gameViewController;
@property (nonatomic, retain) ScoresViewController *scoresViewController;

and in YourAppDelegate.m inside applicationDidFinishLaunching:

// Create the cocos2d view
gameViewController = [[GameViewController alloc] init];
[window addSubview:gameViewController.view];

// Create the high scores view and controller
scoresViewController = [[ScoresViewController alloc] initwithNibName:@"scoresNib"
                                                              bundle:nil];
[window addSubview:scoresViewController.view];

Finally, inside the GameViewController you would have

- (void) loadView {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

    [[Director sharedDirector] attachInView:self.view];

    // Now do your cocos2d scene stuff to start everything off e.g. create a scene
    // and call runWithScene: on the sharedDirector
}

Here, the cocos2d code is running in the background and the scores view is overlaid in front (or hidden until you need it etc).

Then, when your game needs to do something to the high scores, the GameViewController can just get the HighScoresViewController from the app delegate i.e.

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.scoresViewController.view.hidden = NO;

Sam

PS The answers to this question might also be useful to you :)

deanWombourne
Hi,Thanks.This works for me....but is it the only idea...??
Rony