如何在cocos2d中使用过渡进行popScene?

时间:2011-08-11 06:07:30

标签: cocos2d-iphone transitions

当我在cocos2d中调用relpaceScene或pushScene时,我可以添加一些过渡到它:

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

但是当我调用popScene时,它不需要参数,也不能添加任何转换。是这样吗? 如何使用所需的过渡进行popScene?

5 个答案:

答案 0 :(得分:20)

在cocos2d论坛中的Per Guy似乎有效:Link to Forum

在声明 - (void)popScene(第402行)之后将其添加到CCDirector.h:

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

你可以这样调用这个方法:

[[CCDirector sharedDirector] popSceneWithTransition:[CCSlideInRTransition class] durat

答案 1 :(得分:3)

您可以按如下方式使用Category

@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

这样您就不需要修改CCDirector类。

答案 2 :(得分:0)

我修改了Kailash的答案,使用cocos2d 2.1。

<强> CCDirector + additions.h

#import "cocos2d.h"

@interface CCDirector (additions)
-(void)popSceneWithTransition:(Class)transitionClass duration:(ccTime)t;
@end

<强> CCDirector + additions.m

#import "CCDirector+additions.h"

@implementation CCDirector (additions)
-(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

答案 3 :(得分:0)

上面更简洁的版本实际上会释放场景,因此可以根据需要清理它的纹理。

<强> CCDirector + addition.h

#import "cocos2d.h"

@interface CCDirector (addition)

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

@end

<强> CCDirector + addition.m

#import "CCDirector+addition.h"

@implementation CCDirector (addition)

- (void) popSceneWithTransition:(Class)transitionClass duration:(ccTime)t {
    [_scenesStack removeLastObject];

    NSUInteger count = [_scenesStack count];
    NSAssert(count > 0, @"Don't popScene when there aren't any!");

    CCScene* scene = [transitionClass transitionWithDuration:t scene:[_scenesStack lastObject]];
    [self replaceScene:scene];
}

@end

答案 4 :(得分:0)

不必乱用Cocos2d内部,你可以这样做:

CCDirector *director  = [CCDirector sharedDirector];
CCScene *currentScene = [director runningScene];
CGSize contentSize    = [currentScene contentSize];
CCLayerColor *black   = [[CCLayerColor alloc] initWithColor:ccc4(0, 0, 0, 0) width:contentSize.width height:contentSize.height];
[currentScene addChild:black];
[black runAction:[CCSequence actionOne:[CCFadeTo actionWithDuration:1.0f opacity:255]
                                   two:[CCCallFunc actionWithTarget:director selector:@selector(popScene)]]];