cocos2d-x 如何高效率开发游戏
来源:未知 责任编辑:责任编辑 发表时间:2014-03-23 22:25 点击:次
每一个游戏版本的升级都需要预留抽象类接口,所以写抽象类可以大幅度提高编程效率,也可以一劳永逸。在回合制游戏中,写完第一关,再写第二关,如果继续重复代码是不科学的,所这个时候你就要写个抽象类出来,供下面几关的游戏使用;
下面我来跟大家分享下如何去写抽象类,至于其安全性我认为不用担心,我们是在cocos2d-x游戏引擎的基础上去写抽象类,是对cocos2d-x 常用函数的一种封装;
我也在努力往这方面发展,要是写的不好希望大家评论批评;
我写了一个类叫JieKouLayer类这个类可以被游戏所有层继承;
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
class JieKouLayer :public cocos2d::CCLayer
{
public:
JieKouLayer(void);
~JieKouLayer(void);
/***************************公有参数列表**************************************/
cocos2d::CCSpriteBatchNode* _directionBatchNode;
/****************************************************************************/
/*******************获取可视区域*******************************/
cocos2d::CCSize _visibleSize ;
cocos2d::CCPoint _origin ;
void getOriginVis();
/*************************************************************/
/*************************************setCacheBatch*******************************************************/
cocos2d::CCSpriteBatchNode* setCacheAndBatchNode(char* plist,char* pvr);
/****************************************************************************************************/
/***************************设置已存在精灵的属性**********************************/
void setSpritePro(cocos2d::CCSprite* sprite,float posx ,float posy,float scale ,int opacity ,float anrPosx,float anrPosy);
/**********************************************************************************/
};
#include "JieKouLayer.h"
USING_NS_CC;
JieKouLayer::JieKouLayer(void)
{
this->_directionBatchNode=this->setCacheAndBatchNode("dhkey/direction.plist","dhkey/direction.pvr.ccz");
}
JieKouLayer::~JieKouLayer(void)
{
this->_directionBatchNode->release();
}
void JieKouLayer::getOriginVis()
{
this->_visibleSize = CCDirector::sharedDirector()->getVisibleSize();
this->_origin = CCDirector::sharedDirector()->getVisibleOrigin();
}
void JieKouLayer::setSpritePro(cocos2d::CCSprite* sprite ,float posx ,float posy,float scale ,int opacity,float anrPosx,float anrPosy)
{
sprite->setPosition(ccp(posx,posy));
sprite->setScale(scale);
sprite->setOpacity(opacity);
sprite->setAnchorPoint(ccp(anrPosx,anrPosy));
}
CCSpriteBatchNode* JieKouLayer::setCacheAndBatchNode(char* plist,char* pvr)
{
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plist);
CCSpriteBatchNode* batchNode=CCSpriteBatchNode::create(pvr);
batchNode->retain();
this->addChild(batchNode);
return batchNode;
}我们在游戏中包含这个抽象类,然后利用C++的多继承来继承它。
我再补充一点:我们把游戏的缓存资源统一放在抽象类当中去实例化它,就可不不用多次去写cache
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 进入详细评论页>>

![cocos2d_x+lua[2]](/uploads/allimg/131030/110J64609-0-lp.jpg)








