Cocos3D:从CC3Scene调用委托方法

时间:2014-12-27 05:58:43

标签: objective-c cocos3d

在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:?

1 个答案:

答案 0 :(得分:0)

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

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

    ...

}