Sunday, 15 September 2013

Accessing Variables in a sub ccb-file cocos2d-x -



Accessing Variables in a sub ccb-file cocos2d-x -

how access fellow member variables defined in sub-ccb, custom layer class main "ccb" file (cocos2d-x) ?

here base of operations scenario question,

i have ccb file named "ladybirds_page.ccb" , associate custom loader class follows (root cclayer)

ladybirds_pagelayerloader.h ladybirds_pagelayer.h ladybirds_pagelayer.cpp

and have ccb file named "ladybird.ccb" , associate custom loader class follows (root ccnode)

ladybirdloader.h ladybird.h ladybird.cpp

"ladybirds_page.ccb" file contains several instance of "ladybird.ccb". , associated relevant fellow member variables (mladybird_1,mladybird_2,..)

and "ladybird.ccb" self contains several ccsprites associated fellow member variable (mdotsprite_1, mdotsprite_2,...)

i want access these sub-ccb(ladybird)'s fellow member variable each instance of it. like

mladybird_1->mdotsprite_1 mladybird_1->mdotsprite_2

here coding approach far,

create method names setnumber(int num) in ladybird.cpp follows

void ladybird::setnumber(int num) { ....... ......... mdotsprite_1->setvisible(false); mdotsprite_1->setvisible(false); ...... }

mdotsprite_1 , mdotsprite_1 assigned using ccb_membervariableassigner_glue macro

ccb_membervariableassigner_glue(this, "mdotsprite_1", ccsprite *, this->mdotsprite_1); ccb_membervariableassigner_glue(this, "mdotsprite_1", ccsprite *, this->mdotsprite_1);

loading ladybirds_page.ccb file custom loader class

void intro_pagelayer::loadnextpage() { /* create autorelease ccnodeloaderlibrary. */ ccnodeloaderlibrary * ccnodeloaderlibrary = ccnodeloaderlibrary::newdefaultccnodeloaderlibrary(); ccnodeloaderlibrary->registerccnodeloader("ladybird", ladybirdloader::loader()); ccnodeloaderlibrary->registerccnodeloader("ladybirds_pagelayer", ladybirds_pagelayerloader::loader()); /* create autorelease ccbreader. */ cocos2d::extension::ccbreader * ccbreader = new cocos2d::extension::ccbreader(ccnodeloaderlibrary); /* read ccbi file. */ ccnode * node = ccbreader->readnodegraphfromfile("ccb/ladybirds_page.ccbi", this); ccbreader->release(); ccscene * scene = ccscene::create(); if(node != null) { scene->addchild(node); } /* force new scene fancy transition. */ ccdirector::shareddirector()->pushscene(cctransitionpageturn::create(0.5f, scene, false)); }

assign fellow member variable instance of ladybirds in ladybirds_pagelayer.cpp

ccb_membervariableassigner_glue(this, "mladybird_1", ladybird *, this->mladybird_1); ccb_membervariableassigner_glue(this, "mladybird_2", ladybird *, this->mladybird_2);

and finally, seek phone call method setnumber() on mladybird_1 , mladybird_2 follows

mladybird_1->setnumber(1); mladybird_2->setnumber(2);

i got exc_bad_access on

mdotsprite_1->setvisible(false);

obviously, scenario not implemented in code. can please enlighten me right way of doing ?

thank you.

i have same problem loading ccsprite through

ccb_membervariableassigner_glue

macro.

i solved using

ccb_membervariableassigner_glue_weak

macro.

hope helps.

edit

a improve solution utilize

ccb_membervariableassigner_glue

and initialize null variable

variablename = null;

and gently release destructor

cc_safe_release(variablename);

cocos2d-x

No comments:

Post a Comment