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学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1