如何在呈现UIViewController时替换CCScene?

时间:2013-10-28 14:05:29

标签: ios objective-c uiviewcontroller cocos2d-iphone ccscene

我有两个CCScenes,让我们称他们为A和B.当我在场景A时,我用视频播放器呈现UIViewController个实例。然后我想从那个UIViewController转移到场景B.这就是我这样做的方式:

- (void)videoFinished:(NSNotification *)notification
{
[self presentCurrentActivity];

id object = notification.object;

if ([notification.name isEqualToString:AVPlayerItemDidPlayToEndTimeNotification])
    [RAActivityInfoManager setVideoWatched];

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:VIDEO_SKIPPED_NOTIFICATION object:nil];

[[CCDirector sharedDirector] dismissViewControllerAnimated:YES completion:nil];
}

- (void)presentCurrentActivity
{
if (currentActivity >= numberOfActivitiesInLesson)
    currentActivity = 1;

NSString *className = self.activityNames[currentActivity];
Class     class     = NSClassFromString(className);

[RAActivityInfoManager addSpriteFramesForClass:class];

SEL selector   = sel_registerName("scene");
CCScene *scene = [class performSelector:selector];

[[CCDirector sharedDirector] replaceScene:scene];
 }

所有我需要的是在解雇我的UIViewController后进入场景B右。但不幸的是,它总是把我带到场景A片刻,然后才出现场景B.据我所知,显示UIViewControllerCocos2D,以便只有在从CCDirector移除viewController后才能执行此操作。所以我需要找到一种在viewController仍处于打开状态时异步替换CCScene的方法。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

使用cocos2d-iphone扩展程序(https://github.com/cocos2d/cocos2d-iphone-extensions),你应该有更好的运气。您可以尝试使用视频播放器类(CCVideoPlayer)。

答案 1 :(得分:0)

OHK。所以你需要做的是你必须从父母那里删除所有子视图。然后你可以替换你的ccscene。

for (id child in [[[CCDirector sharedDirector] view] subviews]) {
        [child removeFromSuperview];
    }
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[FirstScreen scene] withColor:ccBLACK]];

希望这会对你有所帮助。