How to replace CCScene while UIViewController is presented?

356 Views Asked by At

I have two CCScenes, let's call them A and B. When i'm on the scene A i present an UIViewController instance with video player. Then i want to move from that UIViewController to the scene B. This is how i do that:

- (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];
 }

all that i need is to get onto the scene B RIGHT after dismissing my UIViewController. But unfortunately it always takes me to the scene A for a moment and only after that the scene B is presented. As far as i understand showing UIViewController blocks Cocos2D so that it can act only after the viewController is removed from CCDirector. So i need to find a way to replace CCScene asynchronously while the viewController is still on. Any ideas?

2

There are 2 best solutions below

0
On

Ohk. so what you have to do is you have to remove all sub views from its parents.Then you can replace your ccscene.

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

Hope this will help you.

0
On

You should have better luck getting this to work using cocos2d-iphone extensions (https://github.com/cocos2d/cocos2d-iphone-extensions). There is a video player class (CCVideoPlayer) you can try working with.