Cocos2d-x create custom project template

Source: Internet
Author: User

Have you ever been excited by Cocos for convenient and rapid development, and have you ever been moved by convenient development of various tools? But are you worried about helloworldscene every time you create a new project?


Well, in fact, I also feel that every time I create a project, it is that template, which is too troublesome, because the basic helloworldscene does not need to be deleted, we still need to create some new scene layers by ourselves. Therefore, simply put, we will directly modify this template today, so that it will be convenient to create a project later. In fact, I didn't mean to modify it. Recently I am trying something new, so I often create a new project, but the content of each change is the same, wasting time and effort. Okay. Let's start our operation!


First, we will analyze the basic modules of a game. If we want to make a game or a simple demo, We will basically have these scenarios or layers,

Start scenario

Game scenarios

Splashlayer or the logo image of the game.

The game's end Layer calculates scores and displays several stars.


In addition, another single-instance type of game management, some of the main games are like advertising, payment, and so on.


With the need for these classes, let's start code. Here I use version 3.0, 2. X is similar. Open the more directories of the engine package and enter the Templates folder. cpp-template-default is the template of the CPP project,


The solution for each platform is here. We open the Win32, but an error will occur when it is opened,


Because these libraries are missing, the current version will copy the engine library when creating the project and no longer rely on the original folder structure. Therefore, you can take your full project to any place, so we have to add these libraries, find the project created previously in 3.0, there is a cocos2d folder in it, and then include


Paste it and open our template project. Then you can create necessary classes and write code.

This is the several classes I have created,



The layers folder stores various layers and scenarios. The sprites contains the created genie, but it does not exist, the reason why baselayer is created is that the screen size is obtained every time the layer is created, so I seal it again. The Code is as follows,

#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;}

In this way, the other layers created will inherit the baselayer. Let's take a gamescene code,

#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;}

You must execute the baselayer init method once in the init method. Otherwise, the getwinsize method is not obtained.


Then modify the CPP of appdelegate. The size of the created window is 960*640 by default, which is a little large. If the screen adaptation policy is not filled in, the background music of the program will not be paused, so we can change the code,

# 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); // create a specified window size Director-> setopenglview (glview );} director-> getopenglview ()-> setdesignresolutionsize (1136,640, resolutionpolicy: fixed_height ); // display policy details can be du Niang // turn on display FPS ctor-> setdisplaystats (0); // set FPS. the default value is 1.0/60 if you don't call this ctor-> 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 toovoid appdelegate: applicationdidenterbackground () {Director: getinstance ()-> stopanimation (); // if you use simpleaudioengine, it must be pause simpleaudioengine: getinstance ()-> pausebackgroundmusic (); // pause music, this function will be called when the app is active againvoid appdelegate: applicationwillenterforeground () {Director: getinstance ()-> startanimation (); // if you use simpleaudioengine, it must resume here simpleaudioengine: getinstance ()-> resumebackgroundmusic ();}

OK. These are my main changes. If you have your own ideas, you can modify them.

Close and save our modifications to this solution. In proj. delete the two suffix Files "SDF" and "Suo" in the Win32 Directory, and delete the cocos2d folder that was copied under "CPP-template-Default, let our solutions look like this at first,


This completes. Create a project to see it,

Here I create a project named testtemp, and the solution will be like this,



Success! Hurry up and write the code ~~~




Cocos2d-x create custom project template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.