Cocos2d and Blink after Default.png

Well I finally solved a problem that has been haunting me for some time now. (At least solved it for now). The problem was a bad transition from the default.png to my first scene. And before this sounds like a million other posts on the subject let me say I tried every suggestion out there and nothing worked!! So this is how I did it.

Let me first describe my particular issue. It is not a black flash leading into the first scene, but instead it was a flash of the scene that the app last exited on. If the app exited on the menu scene using the home button obviuously, what would happen was, the next time the app was loaded it would show a flash of that menu scene between the default.png and the initial scene.

I am not sure if this is an underlying problem in the code somewhere else, but as I said I tried everything I could think of, and find, to stop it from happening. I want to share the bit of code that is suggested the most often…i’d say 99% of the time this is suggested on forums and such.

The suggested code

…..put this in you app delegate right before you run the first scene:

#pragma mark Prevent flicker that can occur between the Default image ...

Director *director	= [Director sharedDirector];

NSString *defaultFile	= @"Default.png";
Sprite *defSprite;
CGImageRef defaultImage	= CreateCGImageWithContentsOfScreen();
if(defaultImage){
    CCLOG(@"Redisplay DefaultImage: Got Default.png image for 'free'...
    defSprite = [Sprite spriteWithCGImage:defaultImage key:defaultFile];
}else{
    CCLOG(@"Redisplay DefaultImage: Getting Default.png image from...
    defSprite	= [Sprite spriteWithFile:defaultFile];
}
CGImageRelease(defaultImage);

defSprite.transformAnchor = CGPointZero;

[defSprite draw];

[[director openGLView] swapBuffers];

[[TextureMgr sharedTextureManager] removeTexture:[defSprite texture]];
#pragma mark End of Flicker prevention code you can proceed to run the...

// Run the intro Scene
[director runWithScene: introScene];
//////////////////////////////////////////////////////

Keep in mind this is a simple cut and paste from the website and should be updated for the latest version of Cocos2d.

now that was the version listed on the Cocos2d docs but there are a few other similar versions out there. I am not saying that this does not work for a basic black blink but in my case it did nothing and was getting on my nerve.

So this is what I did;

I did not use that code at all and simply added a Fade transition to the runWithScene in the appDelegate. This simple solution actually solved the problem very nicely, The listed code solution above actually made my problem worse so when I omitted it I got it a little better and after adding the fade transition it solved the problem completely.

So you might be reading this and saying to yourself “man that guy is dumb, That is the most basic thing you should know about” BUT…

After spending time searching on the net this same issue I got a ton of different results explaining this issue…so there must be a lot of people in the same boat.

So to close this post off I will say….give the suggested code a try if you are having difficulties BUT if it does nothing for you or makes it worse….give my ‘Back-to-Basics’ suggestion a go. Maybe it will work perfect for you.

Advertisements

3 Responses to Cocos2d and Blink after Default.png

  1. CJ Hanson says:

    Great blog. Please note that this issue is officially fixed in the latest cocos2d and no longer requires a work-around:
    http://www.cocos2d-iphone.org/wiki/doku.php/tips:director_flicker

  2. sunflowerlab says:

    Good find.. this can be useful for whoever using old cocos2d framework.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: