重新使用Director为新场景

时间:2014-07-02 21:10:01

标签: ios cocos2d-x

我目前正在开发一个Cocos2d-x,它除了Game之外还使用原生UI(UIKit Framework)。

这是我的实际工作流程:

当我开始游戏时,每件事都运作良好。我甚至可以通过重新初始化场景重新启动游戏,但是我无法使用我的CCEAGLView弹出UIViewController并再次将其推到顶部。

当我这样做时,我得到一个没有任何动作的场景。 这是我打开场景:

    if (cocos2d::Director::getInstance()->getRunningScene() && [[self.view.subviews firstObject] isKindOfClass:[CCEAGLView class]]) {

    //Restart scene without popping ViewController
    //Works well
    auto beam = GrowingBeam::createLayer();
    auto scene = beam->getScene();
    beam->_self  = (__bridge void*) self;
    cocos2d::Director::getInstance()->replaceScene(scene);
}else {


    if (cocos2d::Director::getInstance()->getRunningScene()) {

        //Restart scene after View Controller has been popped
        // DOESN'T WORK !!
        auto beam = GrowingBeam::createLayer();
        auto scene = beam->getScene();

        beam->_self  = (__bridge void*) self;

        [self.view insertSubview:(__bridge CCEAGLView *) cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView() atIndex:0];
        cocos2d::Director::getInstance()->replaceScene(scene);
        scene->resume();
        cocos2d::Director::getInstance()->resume();
    }else {

        //Start Scene for the first time
        //Works well
        CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [UIScreen mainScreen].bounds
                                             pixelFormat: kEAGLColorFormatRGBA8
                                             depthFormat: GL_DEPTH24_STENCIL8_OES
                                      preserveBackbuffer: NO
                                              sharegroup: nil
                                           multiSampling: NO
                                         numberOfSamples: 0];

        // IMPORTANT: Setting the GLView should be done after creating the RootViewController
        auto gl2view = cocos2d::GLView::createWithEAGLView((__bridge void*)eaglView);
        cocos2d::Director::getInstance()->setOpenGLView(gl2view);

        [self.view insertSubview:eaglView atIndex:0];

        auto beam = GrowingBeam::createLayer();
        auto scene = beam->getScene();
        beam->_self  = (__bridge void*) self;

        cocos2d::Director::getInstance()->runWithScene(scene);
        cocos2d::Application::getInstance()->run();

    }

}

0 个答案:

没有答案