cocos2d x - Not able to change the background of the scene in cocos2dx android -
i have started game development using cocos2dx. , started helloworld sample game. able run sample game. when try change background color, getting error in
**helloworldscene.h** type 'helloworld' must implement inherited pure virtual method 'cocos2d::ccrgbaprotocol::setopacity' **changes:** class helloworld : public cocos2d::cclayercolor
and in
**helloworldscene.cpp** invalid arguments ' candidates are: bool initwithcolor(const cocos2d::_cccolor4b &, ?, ?) bool initwithcolor(const cocos2d::_cccolor4b &) ' **changes:** cc_break_if(!cclayercolor::initwithcolor(ccc4(255,255,255,255)));
i new cocos2dx , c++. there thing left include or what? please me solve issue. thank you.
edit:
helloworldscene.cpp
#include "helloworldscene.h" using namespace cocos2d; using namespace cocosdenshion; ccscene* helloworld::scene() { ccscene * scene = null; { // 'scene' autorelease object //ccscene *scene = ccscene::create(); scene = ccscene::create(); cc_break_if(!scene); // 'layer' autorelease object helloworld *layer = helloworld::create(); cc_break_if(!layer); // add layer child scene scene->addchild(layer); } while (0); // return scene return scene; } // on "init" need initialize instance bool helloworld::init() { bool bret = false; { ////////////////////////////// // 1. super init first //if ( !cclayer::init()) //{ //return false; //} //cc_break_if(!cclayer::init()); cc_break_if(!cclayercolor::initwithcolor(ccc4(255,255,255,255))); ///////////////////////////// // 2. add menu item "x" image, clicked quit program // may modify it. // add "close" icon exit progress. it's autorelease object ccmenuitemimage *pcloseitem = ccmenuitemimage::create( "closenormal.png", "closeselected.png", this, menu_selector(helloworld::menuclosecallback) ); pcloseitem->setposition( ccp(ccdirector::shareddirector()->getwinsize().width - 20, 20) ); // create menu, it's autorelease object ccmenu* pmenu = ccmenu::create(pcloseitem, null); pmenu->setposition( ccpointzero ); this->addchild(pmenu, 1); ///////////////////////////// // 3. add codes below... // add label shows "hello world" // create , initialize label cclabelttf* plabel = cclabelttf::create("game", "thonburi", 38); // ask director window size ccsize size = ccdirector::shareddirector()->getwinsize(); // position label on center of screen plabel->setposition( ccp(size.width / 2, size.height - 20) ); // add label child layer this->addchild(plabel, 1); // add "helloworld" splash screen" ccsprite* psprite = ccsprite::create("untitled1.png"); // position sprite on center of screen psprite->setposition( ccp(size.width/2, size.height/2) ); // add sprite child layer this->addchild(psprite, 0); ccsprite* player = ccsprite::create("player.png", ccrectmake(0, 0, 27, 50)); player->setposition(ccp(player->getcontentsize().width/2, size.height/2)); this->addchild(player); bret = true; } while (0); return bret; } void helloworld::menuclosecallback(ccobject* psender) { ccdirector::shareddirector()->end(); #if (cc_target_platform == cc_platform_ios) exit(0); #endif }
helloworldscene.h
#ifndef __helloworld_scene_h__ #define __helloworld_scene_h__ #include "cocos2d.h" #include "simpleaudioengine.h" //class helloworld : public cocos2d::cclayer class helloworld : public cocos2d::cclayercolor { public: // here's difference. method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, recommand return class pointer static cocos2d::ccscene* scene(); // selector callback void menuclosecallback(ccobject* psender); // implement "static node()" method manually create_func(helloworld); }; #endif // __helloworld_scene_h__
the best way change background colour do:
glclearcolor(1.0,1.0,1.0,1.0);
during scene init() method. way can skip cclayercolor steps, , bonus of better overall performances. cheers!
Comments
Post a Comment