Cocos2d-x 创建自定义项目模板

时间:2023-02-06 19:07:42

你是否曾经为cocos方便快速开发而兴奋,你是否曾经为各种工具的便利开发而感动,但现在的你是否为每次创建一个新的项目都是HelloWorldScene而苦恼?


好吧,其实我也感觉到了,每次创建一个项目都是那个模板,太麻烦了,因为基本HelloWorldScene不删也是放在那不用的,并且还是要自己创建一些新的scene新的layer,所以,索性一点,今天我们就直接来修改这个模板,这样以后创建项目就方便了。其实也不是故意要修改的,最近在尝试些新的东西,所以经常性的创建一个新项目,但每次刚开始修改的内容都是一样的,浪费时间和精力。好了,话不多说,开始我们的行动!


首先分析一下一个游戏的基本模块,如果要做一个游戏,或者简单一点的一个demo,我们基本会有这些场景或者层,

开始场景

游戏场景

SplashLayer或者说是刚进入游戏的logo画面

游戏结束层,计算分数,展示几个星级这些


除此之外,再来一个游戏管理的单例类,主要游戏中一些像广告啊,支付啊等等什么的全局一个就ok。


有了这些类的需求,咱们开始码一下代码了,这里我用的是3.0的版本,2.x做法差不多,打开引擎包的更目录,进入templates文件夹,cpp-template-default这个里面就是cpp项目的模板了,进来打开一看,

Cocos2d-x 创建自定义项目模板

各个平台的解决方案都在这里了,咱们打开win32下的,但是一打开就会出错的,

Cocos2d-x 创建自定义项目模板

因为缺少这些库,现在的版本创建项目的时候都是会复制引擎库的,不再依赖原来的文件夹结构,所以你可以带着你的完全项目放到任何地方都可以,所以咱们还得添加一下这些库,找到以前用3.0创建的项目,在里面有个cocos2d的文件夹,然后里面包括了这些

Cocos2d-x 创建自定义项目模板

把它粘贴过来,再打开我们的模板项目就可以了,接下来就可以创建必要的类并且写代码了。

这是我建的几个类,

Cocos2d-x 创建自定义项目模板


简单说明一下,Layers文件夹里存放各种层和场景,Sprites里面放创建的精灵,只是现在里面没有,BaseLayer创建的原因是因为每次创建的层里都会用到获取屏幕的大小,所以我又封了一次,代码如下,

#pragma once
#include "cocos2d.h"

USING_NS_CC;

class BaseLayer : public Layer
{
public:
BaseLayer();
~BaseLayer();
CREATE_FUNC(BaseLayer);
virtual bool init() override;

protected:
Size size;

};

#include "BaseLayer.h"

BaseLayer::BaseLayer()
{
}

BaseLayer::~BaseLayer()
{
}

bool BaseLayer::init()
{
if (!Layer::init())
return false;

size = Director::getInstance()->getWinSize();

return true;
}

这样在创建的其他layer就继承BaseLayer就ok,举个GameScene的代码,

#pragma once
#include "cocos2d.h"
#include "Layers\BaseLayer.h"

USING_NS_CC;

class GameScene : public BaseLayer
{
public:
GameScene();
~GameScene();
CREATE_FUNC(GameScene);
virtual bool init() override;
static Scene* createScene();
};

 
#include "GameScene.h"
#include "StartScene.h"
#include "GameManager.h"
#include "GameoverLayer.h"

GameScene::GameScene()
{
}


GameScene::~GameScene()
{
}

Scene* GameScene::createScene()
{
auto scene = Scene::create();
auto layer = GameScene::create();
scene->addChild(layer);

return scene;
}

bool GameScene::init()
{
if (!BaseLayer::init())
return false;



return true;
}

一定要在init方法里执行一次BaseLayer的init方法,不然等于没有获取getWinSize方法。


接着改改AppDelegate这个cpp,默认创建的窗口大小是960*640的,有点大,没有填写适屏策略,程序进入后台音乐不暂停,所以我们改点代码,

#include "AppDelegate.h"
#include "SimpleAudioEngine.h"
#include "Layers\GameScene.h"
#include "Layers\StartScene.h"

USING_NS_CC;
using namespace CocosDenshion;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::createWithRect("My Game", Rect(0, 0, 480, 320)); //创建指定窗口大小
director->setOpenGLView(glview);
}

director->getOpenGLView()->setDesignResolutionSize(1136, 640, ResolutionPolicy::FIXED_HEIGHT); //适屏策略详细可以度娘
// turn on display FPS
director->setDisplayStats(0);

// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object
//auto scene = GameScene::createScene();
auto scene = StartScene::createScene();
// run
director->runWithScene(scene);

return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); //暂停音乐,也就是把这行注释解除
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

ok,这些就是本人主要修改的地方了,如果有自己的想法也是完全可以修改的,看个人了。

关掉并保存我们对这个解决方案的修改,在proj.win32目录下删掉sdf和suo这两个后缀文件,并且把cpp-template-default下之前复制过来的cocos2d那个文件夹也删了,让我们的解决方案最初长得还是那样,

Cocos2d-x 创建自定义项目模板

这样就完成了,赶紧创建一个项目来看看,

这里我创建一个叫TestTemp的项目,解决方案就会像这样了,

Cocos2d-x 创建自定义项目模板


大功告成,赶紧愉快的写代码吧~~~