Cocos3D:从 CC3Scene 调用委托方法



在 Cocos3D 中,如何将自定义 CC3Scene 的回调调用到 UIViewController 上?

我正在关注CC3DemoMultiScene-iOS演示,这是我所做的。

感兴趣的文件: CustomCC3Layer.hCustomCC3Scene.hMainViewController.mMainViewController.h

CustomCC3Layer.h:这个文件中没有什么特别的

CustomCC3Scene.h:我宣布了代表...

@class CustomCC3Scene;
@protocol CustomCC3SceneDelegate <NSObject>
@optional
-(void)sceneCallbackMethod:(CustomCC3Scene *)scene didSelectPart:(NSString*)part;
@end
@interface CustomCC3Scene : CC3Scene {
@property (nonatomic, weak) IBOutlet id< CustomCC3SceneDelegate > delegate;
@end

MainViewController.h: 继承 CustomCC3SceneDelegate

@interface MainViewController : UIViewController<CustomCC3SceneDelegate>
...
@end

MainViewController.m:我创建了3D场景,并作为UIView的子级添加。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:[self createGLView]];
    CC3Backgrounder.sharedBackgrounder.shouldRunTasksOnRequestingThread = YES;
}
-(CCGLView*) createGLView {
    CGRect rect = CGRectMake(0, 0, 200, 300);
    CCGLView* glView = [CCGLView viewWithFrame: rect
                                   pixelFormat: kEAGLColorFormatRGBA8
                                   depthFormat: GL_DEPTH24_STENCIL8
                            preserveBackbuffer: NO
                               numberOfSamples: 1];
    CCDirector* director = CCDirector.sharedDirector;
    director.animationInterval = (1.0f / kAnimationFrameRate);
    director.displayStats = NO;
    director.view = glView;
    // Run the initial static 2D intro scene
    CCScene* scene = [[CustomCC3Layer layer] asCCScene];
    [director runWithScene:scene];
    // I need to set the delegate... but this throws unrecognized selector error
    // CustomCC3Scene* xxx = (CustomCC3Scene*)[[CustomCC3Layer layer] asCCScene];
    // xxx.delegate = self;
    return glView;
}

我试图在 MainViewController.m 中做的是设置 CustomCC3SceneDelegate=self,但这会引发错误。如何正确设置委托,以便调用 sceneCallbackMethod:didSelectPart: ?

使用以下代码解决了我的问题:

-(CCGLView*) createGLView {
    ...
    ...
    CC3Layer* layer = [CustomCC3Layer layer];
    CustomCC3Scene* scene = (CustomCC3Scene*)[layer cc3Scene];
    scene.delegate = self;
    [director runWithScene:[layer asCCScene]];
    ...
}

相关内容

  • 没有找到相关文章

最新更新