當我打電話relpaceScene或pushScene在cocos2d,我可以一些轉換添加到它喜歡:如何使用cocos2d中的轉換popScene?
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1場景:場景]];
但是當我調用popScene時,它不需要參數,並且不能添加任何轉換。是這樣嗎? 我如何popScene與所需的過渡?
當我打電話relpaceScene或pushScene在cocos2d,我可以一些轉換添加到它喜歡:如何使用cocos2d中的轉換popScene?
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1場景:場景]];
但是當我調用popScene時,它不需要參數,並且不能添加任何轉換。是這樣嗎? 我如何popScene與所需的過渡?
每蓋伊在cocos2d的論壇,這似乎工作:Link to Forum
一下添加到CCDirector.h,剛剛宣佈後 - (空)popScene(行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;
}
}
你可以調用的方法是這樣的:
[[CCDirector sharedDirector] popSceneWithTransition:[CCSlideInRTransition class] durat
您可以使用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
類。
我修改了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
上述一個更簡潔的版本實際上釋放的一幕讓它和它的紋理可以根據需要進行清理。
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
無需與混亂的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)]]];
精彩,它的工作。以前我不知道我必須修改cocos2d這個東西。非常感謝。 – willzeng
@willzeng您應該將正確答案標記爲正確,以幫助未來的人。 – ScottPetit
@Scottpetit,爲什麼需要''scenes ['scenesStack_ replaceObjectAtIndex:c-1 withObject:scene];'? – Vasu