转发,请保持地址:http://blog.csdn.net/stalendp/article/details/8757660
前面的文章,我介绍了cocosBuilder的简单集成,这篇将介绍怎么使用cocosBuilder开发物理类的游戏。
机器人模型
先介绍结合机器人模型。如下图,机器人有10个图片组合而成,我们先要在cocosBuilder中把各种图片位置摆正确,然后在代码中读取图片之间的位置等信息,创建对应的body,然后用Revolute joint连成一个整体。
在cocosBuilder中创建一个名为robot的ccb,然后组合组成如下的情况:
调整各个模块的anchor(Chest除外),这些anchor点将被程序读取,作为joint点。然后导出ccbi文件。
在xcode中导入robot.ccbi,然后创建一个robot类,继承于CcbBase类(参见上一片文章《
[cocos2dx开发技巧2]工具CocosBuilder的使用--集成》);覆盖onAssignCCBMemberVariable方法,如下:
bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode) {
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "head", CCSprite *, this->head);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "chess", CCSprite *, this->chess);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "leftArm", CCSprite *, this->leftArm);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "leftHand", CCSprite *, this->leftHand);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "leftLeg", CCSprite *, this->leftLeg);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "leftFoot", CCSprite *, this->leftFoot);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "rightArm", CCSprite *, this->rightArm);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "rightHand", CCSprite *, this->rightHand);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "rightLeg", CCSprite *, this->rightLeg);
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "rightFoot", CCSprite *, this->rightFoot);
return NULL;
}
覆盖onNodeLoaded方法如下:
void onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader) {
CCSpriteBatchNode* robot = CCSpriteBatchNode::createWithTexture(head->getTexture()); //这里将使用SpriteBatchNode来提高性能
addChild(robot);