Forum > C++ > how to change background color with box2d tempalte

how to change background color with box2d tempalte

By intangible Posted 2012-02-13 11:03 Comments 6
intangible
  • Posts: 8

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~

walzer
  • Posts: 1735
  • Location: Xiamen, China

#1 RE: 2012-02-13 13:28

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

Less is More. Keep Focus.

intangible
  • Posts: 8

#2 RE: 2012-02-13 13:54

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.
———
HelloWorld::HelloWorld()
{
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.

darasta
  • Posts: 16

#3 RE: 2013-01-07 21:49

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?

lance_gray
  • Posts: 517
  • Location: Coffee World

#4 RE: 2013-01-08 04:17

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

Check out my blog: sudo make_me_a_game

darasta
  • Posts: 16

#5 RE: 2013-01-08 07:22

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

duckinferno
  • Posts: 1

#6 RE: 2014-03-11 07:25

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()
{
    CCLayerColor::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. :)

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps