开发者

COCOS2D:How to call a nib file from cocos menu and vice versa?

开发者 https://www.devze.com 2022-12-10 09:00 出处:网络
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 froma

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 fr开发者_StackOverflow社区om 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.


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 :)

0

精彩评论

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

关注公众号