how to change background color with box2d tempalte

how to change background color with box2d tempalte
0.0 0


How to change background color with box2d template in cocos2d-x?
I use Chapter 2 tutorial method , but the background is not change.
I’m not only use box2d, but also change the scene’s background color.
So I choose project is cocos2d-x + box2d.

I had tried not in cocos2d-x+box2d template , chapter 2 tutorial method is OK.
But use cocos2d-x+box2d can’t execute.

Who can teach me,thank you~


Have you changed the main layer’s parent class from CCLayer to CCLayerColor, then initialized it with a different color?


First, I appreciate you take the time to help me.
in my HelloWorldScene.h I have change my class from CCLayer to CCLayerColor.
class HelloWorld : public cocos2d::CCLayerColor
then i had added code in HelloWorldScene.cpp.
setIsTouchEnabled( true );
setIsAccelerometerEnabled( true );
if(!CCLayerColor::initWithColor( ccc4(255,255,255,255) ));

but the background have not changed.The color is still black.

it is cocos2d-x + box2d template.
thank you very much.


I have exactly same problem… I use cocos2d-x ;
i did exactly

  1. In HelloWorldScene.h
    class HelloWorld : public CCLayerColor

  2. In HelloWorldScene.cpp
    bool HelloWorld::init() {
    if (CCLayerColor::initWithColor( ccc4(0,0,255,255) )
    // …

it doesnt change anything i tried with different opacity and different rgb values?
why can it be?


You have to call this -> setColor ); on the *init()* method.


Lance Gray wrote:

You have to call *this > setColor );* on the *init* method.
Thanks for answer but first of all setColor takes a different parameter.
virtual void setColor
Second, when i try this
>getColor(), i see that R,G,B is changed but just screen color did not


I’m using marmalade, which is currently using cocos2d-2.1beta3-x-2.1.0.

I had the exact same issue, using a CCLayerColor that would not change from a black screen/background, regardless of using initWithColor() or setOpacity() or setColor(). Getters also indicated that the colour was being properly set in the layer’s data, just not actually showing up.

The cause was that I was overriding draw, which was set up by default in the blank marmalade cocos2d-x project. The fix is simply to call CCLayerColor’s draw method first.

void GameLayer::draw()
	// Now draw your custom stuff

Apologies for the thread necro, but I had the same problem and have been searching far and wide for an explanation. I just happened to figure it out myself and want to leave this here for anyone else confused. :slight_smile: