Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to popScene with transitions in cocos2d?

When i call relpaceScene or pushScene in cocos2d, I can add some transitions to it like:

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1 scene:scene]];

but when i call popScene, it takes no parameters, and no transition can be added. Is that so? How can i popScene with desired transitions?

like image 880
willzeng Avatar asked Aug 11 '11 06:08

willzeng


2 Answers

Per Guy in the cocos2d forums this seems to work: Link to Forum

Add this to CCDirector.h, just after the declaration -(void)popScene (line 402):

    - (void) popSceneWithTransition: (Class)c duration:(ccTime)t;

Then add this to CCDirector.m, just after the definition of -(void)popScene (line 768):

    -(void) popSceneWithTransition: (Class)transitionClass duration:(ccTime)t;
    {
    NSAssert( runningScene_ != nil, @"A running Scene is needed");

    [scenesStack_ removeLastObject];
    NSUInteger c = [scenesStack_ count];
    if( c == 0 ) {
        [self end];
    } else {
        CCScene* scene = [transitionClass transitionWithDuration:t scene:[scenesStack_ objectAtIndex:c-1]];
        [scenesStack_ replaceObjectAtIndex:c-1 withObject:scene];
        nextScene_ = scene;
    }
}

You can call the method like this:

[[CCDirector sharedDirector] popSceneWithTransition:[CCSlideInRTransition class] durat
like image 189
ScottPetit Avatar answered Oct 10 '22 04:10

ScottPetit


You can use Category as follows:

@interface CCDirector (PopTransition)

- (void)popSceneWithTransition:(Class)transitionClass duration:(ccTime)t;

@end

@implementation CCDirector (PopTransition)

- (void)popSceneWithTransition:(Class)transitionClass duration:(ccTime)t {

    [self popScene];

    // Make Transition
    if (nextScene_) {
        CCScene* scene = [transitionClass transitionWithDuration:t scene:nextScene_];
        [scenesStack_ replaceObjectAtIndex:([scenesStack_ count] - 1) withObject:scene];
        nextScene_ = scene;
    }
}

@end

This way you need not to modify CCDirector class.

like image 3
Vasu Avatar answered Oct 10 '22 03:10

Vasu