Cocos2d-x create custom project template, cocos2d-x Template

Source: Internet
Author: User

Cocos2d-x create custom project template, cocos2d-x Template

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 ~~~





How to create a project in cocos2d-x-222

Since the cocos2d-x2.2, you must use the python script to create a project, only in this way can you create multiple IDE projects at a time, these projects are used for VS, Eclipse, XCODE and other IDE, respectively, however, the project names are the same and the source code files in the project are the same,

First install the python software package corresponding to your operating system, search and download it online
Then go to the command line status,
\ $ Cd cocos2d-x/tools/project-creator
\ $./Create_project.py project PROJECT_NAMEpackage PACKAGE_NAME ~~ Language PROGRAMING_LANGUAGE

Read this article
Www.cocos2d-x.org/..d_line

Why is the helloworld template always created in python when creating a cocos2d-x project?

The default value is
Helloworld
Template.

Related Article

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.