1

I am building an android app..... I tried using cc.color(,,) but isnt working as expected... Could someone tell me how to change the layer background colour?? Thanks.

var GameScene = cc.Scene.extend({
onEnter:function () {
    this._super();
    var layer = new GameLayer();
    this.addChild(layer);

    backgroundLayer = new cc.LayerColor.create();
    backgroundLayer.changeWidthAndHeight(winSize.width, winSize.height);
    backgroundLayer.setColor(cc.c3b(144, 192, 248));
    layer.addChild(backgroundLayer);
    backgroundLayer.setPosition(cc.p(winSize.width * 0.5, winSize.height *       0.5));


}
});
kaushik reddy
  • 167
  • 3
  • 16

2 Answers2

2

You don't have to add a new layer/node to change the background color of a layer. You can simply change the background color of the GameLayer itself by extending LayerColor and calling the appropriate super constructor from within the init function:

var GameLayer = cc.LayerColor.extend({
    init:function () {
        this._super(cc.color.WHITE);
        //...
    }
};
Markus Ende
  • 830
  • 1
  • 9
  • 21
0

First of all, you need to create LayerColor and add it to the scene as a background to color it. The controller/rootNode layer is only Layer, not LayerColor and you can't set the specific one.

backgroundLayer = new cc.LayerColor.create();
backgroundLayer.changeWidthAndHeight(winSize.width, winSize.height);
backgroundLayer.setColor(cc.c3b(144, 192, 248));
controller.addChild(backgroundLayer);
backgroundLayer.setPosition(cc.p(winSize.width * 0.5, winSize.height * 0.5));
sortris
  • 207
  • 3
  • 8
  • I have used this code and there is no change in the background colour...I have edited the question,the way am using it..is that correct? @sortris – kaushik reddy Mar 21 '15 at 12:49
  • change line: cc.addChild(backgroundLayer); to layer.addChild(backgroundLayer); – sortris Mar 21 '15 at 17:38
  • I am using v3.3 @ Sebastián Vansteenkiste .... i made the change but still there is no change in layer colour.. @ sortris – kaushik reddy Mar 21 '15 at 19:44
  • I have fixed that issue....Could you help me out with this one http://stackoverflow.com/questions/29193210/setting-ui-button-width-and-height-in-cocos2d-js-android-application @ sortris – kaushik reddy Mar 23 '15 at 02:37
  • If you fixed that issue please post how you did it. For the next ones :) – sortris Mar 23 '15 at 13:27
  • I started drawing a rectangle with required colour as background instead of changing layer colour....Can we share game score through whatsapp,facebook,twitter or any social sharing options in cocos2d js??? http://stackoverflow.com/questions/29183011/social-sharing-with-cocos2d-js-android-application @ sortris – kaushik reddy Mar 23 '15 at 17:24