The abstract class interface must be reserved for every game version upgrade. Therefore, writing abstract classes can greatly improve programming efficiency, and can be used once and for all. In a turn-based game, it is unscientific to write the first level and the second level. At this time, you need to write an abstract class for the following levels of games;
Next I will share with you how to write abstract classes, as for its security I think don't worry, we are on the basis of the cocos2d-x game engine to write abstract classes, is a kind of encapsulation of common functions of cocos2d-x;
I am also trying to develop in this area. If I cannot write well, I hope you will comment and criticize me;
I wrote a class called the JieKouLayer class which can be inherited by all layers of the game;
# 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 ); /*************************** public parameter list ********* * ***************************/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;}
We include this abstract class in the game, and then inherit it using the multi-inheritance of c ++.
I want to add another point: we put the game cache Resources in the abstract class to instantiate it, so we don't have to write the cache multiple times.