setContentSize is not working in cocos2dx3.0


setContentSize is not working

i am new in cocos2dx and i want to add layer from another class to my main Splashscreen class but setContentSize use device width and height.

i want to set layer size along with my sprite size.

see my code


#include "Gadget.h" #include "math.h" int nCount = 100; static CCPoint Circle [100]; // vertex array Gadget* Gadget::create(int type) { Gadget *pRet = new Gadget(); if (pRet && pRet->init(type)) { pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; return NULL; } } bool Gadget::init(int type) { if(!LayerColor::init()) { return false; } this->setContentSize(CCSize::ZERO); this->initWithColor(Color4B(255,255,255,255)); CCLog("Before Calling"); addGadget(type); return true; } Gadget::Gadget() { // TODO Auto-generated constructor stub } Gadget::~Gadget() { // TODO Auto-generated destructor stub } void Gadget::addGadget(int type) { const char* image = "gadget_2.png"; if(type==2) { image = "gadget_1.png"; } //this->initWithFile(image); CCLog("After Calling"); CCSprite *spr = CCSprite::create(image); setGadgetPos(this->getContentSize().width/2,0.0); this->drawCircle(100,100,100); } void Gadget::setGadgetPos(float x, float y) { // this->ignoreAnchorPointForPosition(true); this->setPosition(x,y); } void Gadget::gadgetAnimStart(int type) { if(type==1) { this->setAnchorPoint(ccp(0,3)); auto rotate = RotateBy::create(3,360); this->runAction(CCRepeatForever::create(rotate)); } } void Gadget::gadgetAnimStop() { this->stopAllActions(); } void Gadget::drawCircle(float X, float Y,float rad) { DrawNode *drawnode = DrawNode::create(); for ( int i = 0 ; i <100; i ++) { float rads = i * M_1_PI; // radians Circle [i] .x = rad * cosf (rads); //vertex x Circle [i] .y = rad * sinf (rads); //vertex y } drawnode->setPosition(Director::sharedDirector()->getVisibleSize().width/2,Director::sharedDirector()->getVisibleSize().height/2); drawnode->drawPolygon(Circle,100,Color4F(0,0,0,0),1,Color4F(1,122,153,1)); this->addChild(drawnode); }

i have to use Gadget.coo class at Splashscreen.cpp

#include "Splashscreen.h" #include "HelloWorldScene.h" #include "CCHelper.h" #include "Gadget.h" #include "UserDefaultStorage.h" #include "VisibleRect.h" #include "math.h" #include "Sound.h" #include "Text_En.h" USING_NS_CC; Scene* Splashscreen::createScene() { Scene *s = Scene::create(); LayerColor *lc = Splashscreen::create(); s->addChild(lc); return s; } Splashscreen::Splashscreen() { // TODO Auto-generated constructor stub } Splashscreen::~Splashscreen() { // TODO Auto-generated destructor stub } bool Splashscreen::init() { if(!LayerColor::create()) { return false; } this->initWithColor(ccc4(0,0,0,255)); this->setContentSize(CCSizeZero); Gadget *g = Gadget::create(GREEN_GADGET); g->setPosition(ccp(-100,-100)); this->addChild(g); return true; }

when i use "this->setContentSize(CCSizeZero);" code but no changes in view. its generate fullscreen layer from position -100,-100

please give me some solutions why setContentSize is not working.

Thanks in advanced Rishabh Shah


After initialization setContentSize will set size but won't make any visible change, better you provide width and height at initialization.


this->initWithColor(ccc4(0,0,0,255), width, height);


if(!LayerColor::create()) { return false; }

It should be

if(!LayerColor::init()) { return false; }


  • GetBodyCount() always returning 0 in Box2d?
  • Cocos2d-x creating an object based upon CCLayerColor
  • iOS Localization Doesn't Work with More Than 63 Files
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Ensure fsync did its job
  • GridView breaks while scrolling
  • MongoDb aggregation
  • How to use RequestBodyAdvice
  • Set the selected item in dropdownlist in MVC3
  • Error when parsing timestamp with pandas read_csv
  • Is it possible to access block's scope in method?
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • C++ Partial template specialization - design simplification
  • How to use an array of arrays with array_map(…) in PHP?
  • QLineEdit password safety
  • Different response to non-authenticated users and AJAX calls
  • jQuery show() function is not executed in Safari if submit handler returns true
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • Eraser for UIBezierPath
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Why winpcap requires both .lib and .dll to run?
  • AngularJs get employee from factory
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?