When using a sub ccb file associate with a custom clase, we need add a custom loader first.
Let's name it FriendLoader
, there is the code in FriendLoader.h:
#include "Friend.h"
/* Forward declaration. */
class CCBReader;
class FriendLoader : public cocos2d::extension::CCLayerLoader {
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(FriendLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(Friend);
};
And register it in AppDelegate::applicationDidFinishLaunching()
:
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
ccNodeLoaderLibrary->registerCCNodeLoader("Friend", FriendLoader::loader());
or some other place you like.
Then we can use the custom class(Friend
) in ccb file. Open Friend.ccb file, select the root node, and fill in the 'Custom class' blank with Friend
.
Then select the CCLabelTTF
named m_friend_name
, and select the 'Doc root var', this is very important ! ! !.
Here I want give a simple explain.
Since in FriendList
class, it reads node from ccb file with reader.readNodeGraphFromFile("FriendList.ccbi", this);
, and using itself as a 'owner', so these variables in it are 'owner var'. When reading nodes, the CCBReader
will assign these 'owner var' to this 'owner' that is a FriendList
instance directly.
While Friend
is a custom class used in CocosBuilder, it is the root node in Friend.ccb, and the variable in it is 'Doc root var'. When reading nodes, the CCBReader
will first read the instances of Friend
, then assign these 'Doc root var' to these instances.
So what make me failed before is indeed these, I select 'owner var' of all of the variables in both FriendList.ccb and Friend.ccb.
Then CCBReader
assign the variable m_friend_name
in sub Friend.ccb to the 'owner'( an instance of Friend
).
For more information, can see CocosBuilder: Connecting with cocos2d-x.