Cocos2d-x Create a custom project template __cocos2d-x

Source: Internet
Author: User

Have you ever been excited about cocos for easy and fast development, and have you ever been moved by the convenience of various tools, but now you are helloworldscene to create a new project every time.


All right, in fact, I also feel that every time you create a project is that template, too troublesome, because the basic helloworldscene do not delete is also placed in the use, and still want to create some new scene new layer, so, simply a little, today we will directly modify this template , so it's convenient to create the project later. In fact, is not deliberately to modify, and recently tried something new, so often create a new project, but every time just start to modify the content is the same, wasting time and energy. Well, say nothing more, and begin our action.


First of all, analyze a game of the basic module, if you want to do a game, or a simple demo, we will basically have these scenes or layers,

Start scene

Game scene

Splashlayer or a logo that just entered the game.

Game end layer, calculate score, show a few stars these


In addition, a single example of game management class, the main game Some like ads ah, pay ah, and so what the global one is OK.


With the needs of these classes, we start code code, here I use the version of 3.0, 2.x approach, open the more directory of the engine pack, into the Templates folder, Cpp-template-default This is the CPP project template, come in and open a look,


The solution for each platform is here, let's open the Win32, but it's going to go wrong.


Because of the lack of these libraries, the current version of the build project will replicate the engine library, no longer rely on the original folder structure, so you can take your complete project anywhere can be, so we have to add these libraries, Find a project that was previously created in 3.0, with a cocos2d folder inside it, and then include these


Paste it over, then open our template project, then you can create the necessary classes and write the code.

This is a few of the classes I built,



To put it simply, the Layers folder contains a variety of layers and scenes, sprites inside the creation of the wizard, but now there is no, Baselayer created because each created layer will use to get the size of the screen, so I sealed 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;
}

So in the creation of other layer inherit Baselayer OK, give 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;
}

Be sure to perform a baselayer init method in the Init method, or else you will not get the Getwinsize method.


Then change appdelegate This CPP, the default window size is 960*640, a bit large, do not fill in the appropriate screen strategy, the program into the background music does not pause, so we a pity Dorado 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));
    Creates the specified window size director->setopenglview (Glview);  } director->getopenglview ()->setdesignresolutionsize (1136, 640, resolutionpolicy::fixed_height);

    Appropriate screen strategy details can be Niang//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 ();
    RunDirector->runwithscene (Scene);
return true; }//This function would be called when the app is inactive. When comes a phone call,it ' is invoked too void Appdelegate::applicationdidenterbackground () {director::getinstance (

    )->stopanimation ();  If use Simpleaudioengine, it must be pause simpleaudioengine::getinstance ()->pausebackgroundmusic (); Pause the music, that is, to remove this line of comment from the}//This function would be called when the app is active again void Appdelegate::applicationwillenterfo

    Reground () {director::getinstance ()->startanimation ();
If use Simpleaudioengine, it must resume Simpleaudioengine::getinstance ()->resumebackgroundmusic ();
 }

OK, these are my main changes in the place, if you have their own ideas can also be completely modified, look at the individual.

Turn off and save our changes to the solution, In the Proj.win32 directory deleted SDF and suo these two suffix files, and the cpp-template-default copied before the Cocos2d folder has also been deleted, so that our solution initially look like that,


This is done, and quickly create a project to see,

Here I create a project called Testtemp, and the solution will be like this,



Finished, hurriedly happy write code ~ ~ ~




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.