iPhone - 卸载在View Controller中运行的cocos2d游戏

时间:2010-08-05 19:26:45

标签: iphone cocos2d-iphone

所以这就是交易,我一直在以开源tweeJump为基础制作游戏,作为大型应用程序的一小部分。

我已成功加载游戏(从tableView)并且能够播放它,但我不太确定如何停止运行cocos2d并返回到启动游戏的表视图。我已经尝试在cocos2d中实现一个停止CCDirector的按钮,但这基本上只是冻结我的应用程序 - 我不太确定如何弹出视图控制器。我对cocos2d并不熟悉,所以我很感激任何帮助。感谢。

以下是一些相关代码:

appDelegate(这不会运行游戏,但设置CCDirector供以后使用):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

[application setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES]; 

[[CCDirector sharedDirector] setPixelFormat:kRGBA8];
[[CCDirector sharedDirector] setAnimationInterval:1.0/kFPS];

[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888]; 

[window makeKeyAndVisible];

return YES;
}

以下是运行游戏的视图控制器(我从tableViewController加载)的相关实现,一切都在loadView中:

(void)loadView{
if (!load) {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
load = YES;
[[CCDirector sharedDirector] attachInWindow:window];
[window makeKeyAndVisible];
CCScene *scene = [[CCScene node] addChild:[Game node] z:0];
[[CCDirector sharedDirector] runWithScene: scene];
}
}

2 个答案:

答案 0 :(得分:-1)

难道你不能让你的TableView成为一个单独的cocos2d场景,然后只是切换场景吗?

这些是我在应用程序委托中切换场景的示例方法:

- (void)loadGameOverScene {
    [_gameOverScene.layer.label setString:@"You Lose :["];
    [_gameOverScene.layer reset];
    [[CCDirector sharedDirector] replaceScene:_gameOverScene];
}

- (void)loadWinScene {
    [_gameOverScene.layer.label setString:@"You Win!"];
    [_gameOverScene.layer reset];
    [[CCDirector sharedDirector] replaceScene:_gameOverScene];
}

- (void)loadNewLevelScene {
    [_newLevelScene.layer reset];
    [[CCDirector sharedDirector] replaceScene:_newLevelScene];
}

- (void)nextLevel {
    [_mainScene.layer reset];
    [[CCDirector sharedDirector] replaceScene:_mainScene];
}

- (void)restartGame {
    _curLevelIndex = 0;
    [self nextLevel];
}

- (void)levelComplete {    

    _curLevelIndex++;
    if (_curLevelIndex >= [_levels count]) {
        _curLevelIndex = 0;
        [self loadWinScene];
    } else {
        [self loadNewLevelScene];
    }

}

此外,cocos2d还有一个菜单系统:CCMenu和CCMenuItem。

以下是有关如何使用不同类型菜单的教程:

http://www.raywenderlich.com/414/how-to-create-buttons-in-cocos2d-simple-radio-and-toggle

我喜欢Ray的教程。希望这有帮助!

答案 1 :(得分:-1)

我用它来打开cocos2d中的GameCenter leaderboard。

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] 

    init];
        if (leaderboardController != NULL)
        {
            leaderboardController.leaderboardDelegate = self;
            AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

            [[app navController] presentModalViewController:leaderboardController animated:YES];

            [leaderboardController release];
        }

只需调用viewController代替leaderboardController就可以了。