Fix for cocos2d color banding

In my first tutorial, I brought up an issue with color banding in gradient pngs used in Sprites in cocos2d. I mentioned a hack that I was using that involved changing a line in the Textures2D class. But I just discovered a better way. These two lines seem to take care of it:

[c][[Director sharedDirector] setPixelFormat:kRGBA8];
[Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];[/c]

And you don’t have to touch the cocos2d source at all.

Note, this works with the currently checked out 0.8 version of the library. Can’t say how it will work with the 0.7.x version.

This entry was posted in iPhone, Objective C. Bookmark the permalink.

8 Responses to Fix for cocos2d color banding

  1. dan says:

    Loads better, thanks kp. Loving these Cocos2d articles ..

  2. Keith Peters says:

    Just realized that this is only an issue in version 0.8.

    In 0.7.3, which was just released, the default pixel format is RGBA8888, so all you have to do is call the first line on the Director – setPixelFormat:kRGBA8.

    In 0.8, default pixel format defaults to RGBA4444. So you may run into banding at that point and the above is the fix.

  3. egarayblas says:

    I’m sorry but I’m new to all these…where exactly do I put those 2 lines of code mentioned above? Thanks in advance! Great tutorials! 🙂

  4. Awesome work! That little issue was annoying me.

    egrayblas – i inserted it in the appdidfinishlaunching method but just make sure it is the first call to the director or you’ll get an error that says somehting like – cant set the default when its already running

    eg line 5 of the second PLAIN TEXT example on this page http://www.bit-101.com/blog/?p=2115

  5. Daniel says:

    I tried this and it doesn’t work. I think I have a newer version of cocos2d.

    1. Director needs to be CCDirector and Texture2D needs to be CCTexture2d.
    2. CCDirector does not have a method for setting pixel format!

    ???

    the first line of code works if u change it to CCTexture2d.
    but the second line doesn’t at all.

Leave a Reply