CCDirecter 用 UIView/nib 替换场景场景、CCDirecter、UIView、nib

2023-09-06 14:55:22 作者:Exile つ (流放fan)*

我想做的是让 Cocos2d 共享导演用 UIKit 中的 UIView 替换当前场景(可能从笔尖加载).这个想法是在 Cocos2d 和 UIKit 之间进行干净的转换.

What I would like to do is have the Cocos2d shared director to replace the current scene with a UIView from UIKit (possibly loaded from a nib). The idea being to cleanly transition between Cocos2d and UIKit.

我已经看到了一些解决这个问题的方法.其中大部分是关于在同一场景中将 UIKit 与 Cocos2d 混合,我想将它们分开.一种方法是添加 UIView 的实例作为导向器 GLView 的子视图.像这样:

I've seen a few approaches to this problem. Most of them are about mixing UIKit with Cocos2d in the same scene, I'd like to keep them separate. One approach is to add an instance of UIView as a subview of the directors GLView. Like so:

UIView* cocosView = [[CCDirector sharedDirector] openGLView];
[cocosView addSubview:t];

将 UIView 叠加到 Cocos 层上?

这可行,但我必须先创建一个空白场景并过渡到该场景,然后添加 UIView,然后将其删除并过渡到任何其他场景.这是模块化的,但有点混乱.

This works but I would have to create a blank scene and transition to that first, then add the UIView, then later remove it and transition to whatever other scene. That's modular but a little messy.

更好的方法是将 UIView 包装到节点中.就像是:http://www.cocos2d-iphone.org/forum/topic/6889

A better approach is to wrap a UIView into a node. Something like: http://www.cocos2d-iphone.org/forum/topic/6889

有官方的 cocos2d 扩展吗?如果可能的话,我还想从故事板/笔尖加载 UIView.非常感谢.

Is there an official cocos2d extension for this? I'd also like to load the UIView from storyboard/nib if possible. Much obliged.

推荐答案

用 UIKit 中的 UIView 替换当前场景

replace the current scene with a UIView from UIKit

如果你使用 cocos2d 1.x:http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

if you are using cocos2d 1.x: http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

如果你使用 cocos2d 2.x:CCDirector 是 UIViewController 的子类,所以你可以使用 常用方法.停止后的增量时间计算存在一点错误/开始动画,但很容易修复.

if you are using cocos2d 2.x: CCDirector is a subclass of UIViewController, so you can use usual methods. There's a little bug with delta time calculation after stopping/starting animation, but it's easy to fix.