Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am testing transitions in cocos2d, and am running into a problem.

I have a MainMenu screen which contains a button. The idea is that clicking on the button will trigger a CCTransitionFadeTR transition to a WhackMain screen where there is a CCLabelTTF which displays "Hello World".

But when I press the button and start the transition, the WhackMain screen appears at full opacity for a moment before the transition from MainMenu to WhackMain begins. The transition appears to be working correctly, apart from WhackMain being visible for a moment before the transition begins.

How can I keep this from happening?

Here is my code, if it will help identify the problem:

MainMenu.m:

#import "MainMenu.h"
#import "WhackMain.h"

@implementation MainMenu

+(CCScene *) scene1 {
// 'scene' is an autorelease object.
CCScene *scene1 = [CCScene node];

// 'layer' is an autorelease object.
MainMenu *layer1 = [MainMenu node];

// add layer as a child to scene
[scene1 addChild: layer1];

// return the scene
return scene1;
}


-(id) init {
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

    CGSize winSize = [[CCDirector sharedDirector] winSize];

    // Create a label for display purposes
    _label = [[CCLabelTTF labelWithString:@"Last button: None" 
                               dimensions:CGSizeMake(320, 50) alignment:UITextAlignmentCenter 
                                 fontName:@"Arial" fontSize:32.0] retain];
    _label.position = ccp(winSize.width/2, 
                          winSize.height-(_label.contentSize.height/2));
    [self addChild:_label];

    // Standard method to create a button
    CCMenuItem *starMenuItem = [CCMenuItemImage 
                                itemFromNormalImage:@"ButtonStar.png" selectedImage:@"ButtonStarSel.png" 
                                target:self selector:@selector(starButtonTapped:)];

    // position the label on the center of the screen
    starMenuItem.position =  ccp( winSize.width /2 , winSize.height/2 );



    CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
    starMenu.position = CGPointZero;
    [self addChild:starMenu];
}
return self;
}

- (void)starButtonTapped:(id)sender {
  [_label setString:@"Last button: *"];
  [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR
                                           transitionWithDuration:2 scene:[WhackMain scene2]]];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc {
[super dealloc];
}

@end

WhackMenu.m:

#import "WhackMain.h"
#import "MainMenu.h"

@implementation WhackMain

+(CCScene *) scene2 {
// 'scene' is an autorelease object.
CCScene *scene2 = [CCScene node];

// 'layer' is an autorelease object.
WhackMain *layer2 = [WhackMain node];

// add layer as a child to scene
[scene2 addChild: layer2];

// return the scene
return scene2;
}

-(id) init {
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

    // create and initialize a Label
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

    // ask director the the window size
    CGSize size = [[CCDirector sharedDirector] winSize];

    // position the label on the center of the screen
    label.position =  ccp( size.width /2 , size.height/2 );

    // add the label as a child to this Layer
    [self addChild: label];
}
return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc {
[super dealloc];
 }


@end
share|improve this question
    
Dear downvoters.. please leave at least a comment about your reasoning. –  bummzack Feb 11 '12 at 22:39
    
The question is too localized. –  Gustavo Maciel Feb 11 '12 at 22:44
    
@PeterK Did you try other transitions (other than CCTransitionFadeTR) as well? Do they show the same behavior? –  bummzack Feb 11 '12 at 22:45
    
@Gtoknu I don't think it is. Maybe the OP posted unnecessary amounts of code, but the problem might be something that could be of interest for several cocos2d users. –  bummzack Feb 11 '12 at 22:50
3  
The question isn't too localised -- it's just worded in a way that's referring to the outcome of the specific problem that PeterK's seeing, instead of to a generic problem that someone else may well also be having. A minor rewrite making the question about transitions instead of being about "Hello World" would probably help it a lot. –  Trevor Powell Feb 11 '12 at 22:58
show 6 more comments

1 Answer

up vote 0 down vote accepted

Well I tried to recreate your problem in one my projects but it doesn't appear for my case.

There are 2 things you can do here.
1. Install new cocos2d framework,
2. If 1st one doesn't work you can schedule a timer that initializes all your nodes in the given scene after 1 sec like this

[self schedule:@selector(somefunction:) interval:1.0f];

Which is also not a bad choice as when changing the scene the memory usage is at peak and in this way you will reduce the risk of crashing the app due to low memory warnings.

Mostly people use a loading screen which contains nothing more than a label but if you are have problems in transition than i would say go for the second option even if 1st one works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.