Cocos2d-x creating an object based upon CCLayerColor -


cocos2d-x 2.1rc0 os x 10.8, xcode 4.6.2

playing around hellowworld box2d example gain concepts.

creating class extension of cclayercolor.

previously, before created separate object doing:

// background cclayercolor *background = cclayercolor::create(cghostwhite); background->setcontentsize(ccsizemake(1024, 768)); background->setposition(0,0); this->addchild(background,0); 

this worked. after trying create own object getting , error:

error: no viable conversion 'plainbackgroundlayer::plainbackgroundlayer' 'plainbackgroundlayer::plainbackgroundlayer *' 

here doing:

plainbackgroundlayer.h:

#ifndef __plainbackgroundlayer_h__ #define __plainbackgroundlayer_h__  #include "cocos2d.h" #include "box2d.h"  class plainbackgroundlayer : public cocos2d::cclayercolor  {      public:         plainbackgroundlayer(cocos2d::cccolor4b incolor);         ~plainbackgroundlayer();          virtual void draw();      private:         cocos2d::cccolor4b backgroundcolor;         cocos2d::ccsize layersize;         cocos2d::cclayercolor *background; };  #endif // __plainbackgroundlayer_h__ 

plainbackgroundlayer.cpp:

#include "plainbackgroundlayer.h"  using namespace cocos2d;  plainbackgroundlayer::plainbackgroundlayer(cocos2d::cccolor4b incolor) {     layersize = ccdirector::shareddirector()->getwinsize();      backgroundcolor = incolor;      background = cclayercolor::create(backgroundcolor);     background->setcontentsize(ccsizemake(1024, 768));     background->setposition(0,0); }  plainbackgroundlayer::~plainbackgroundlayer() {   delete background;    } 

and instantiating like:

 plainbackgroundlayer::plainbackgroundlayer *background = plainbackgroundlayer::plainbackgroundlayer(cghostwhite);  this->addchild(background,0); 

what doing wrong? feel doing correctly.

update 1: doing:

in .cpp:

static plainbackgroundlayer* plainbackgroundlayer::create(cocos2d::cccolor3b incolor) {     // create functions should return autoreleased objects.     plainbackgroundlayer* layer = new plainbackgroundlayer();     layer->setcolor(incolor);     return layer->autorelease();    } 

in .h:

class plainbackgroundlayer : public cocos2d::cclayercolor  {     public:         static plainbackgroundlayer* create(cocos2d::cccolor3b &var);          virtual void draw(); }; 

and getting errors in .cpp:

`out-of-line definition of 'create' not match declaration in 'plainbackgroundlayer'`  `'static' can specified inside class definition`  `cannot initialize return object of type 'plainbackgroundlayer *' rvalue of type 'cocos2d::ccobject *'` 

notice both subclassing cclayercolor, , have member variable called 'background' cclayercolor. suspect might misunderstanding how inheritance works. want 1 or other. might @ 'is a' vs 'has a' relationships if case.

on top of this, don't add 'background' member variable scene, have no effect. shouldn't delete ccnode based objects, , don't need call release on most, since autoreleased , managed scene.

what want remove background member variable, , define new create method this, constructor nothing. (note:i haven't tested code):

// goes in plainbackgroundlayer.h's public method section. static plainbackgroundlayer* create(cccolor3b &var);  // in cpp: (edit: removed static keyword, , make instances set size/pos) plainbackgroundlayer* plainbackgroundlayer::create(cccolor3b &var) {    // create functions should return autoreleased objects.    plainbackgroundlayer* layer = new plainbackgroundlayer();    layer->setcolor(var);    layer->setcontentsize(ccsizemake(1024, 768));    layer->setposition(0,0);    return layer->autorelease();  }  // can omit , use default constructor well.  want point out // constructor doesn't need plainbackgroundlayer::plainbackgroundlayer() {  } 

the advantage of inheritance can treat derived classes in similar fashion. can instantiate , add scene same way did before:

// background plainbackgroundlayer *background = plainbackgroundlayer::create(cghostwhite); this->addchild(background,0); 

Comments