Cocos2dx learning report 2

Source: Internet
Author: User
Record 2: Create a project named dfj)

In the previous record, we talked about the development environment configuration and how to build your own project. Now we can build a project using the method described above. I will stop here.

After creating our own project, we can start to write our own code. Here I do not modify the existing helloworldscene file. We build our own scenarios to start our own game journey.

First of all, it is explained here that most of the subsequent records will be presented to you in the form of code, I will give you a description of what needs to be recorded.

---------- Task recorded in this record-complete the start scene. The start interface has been redirected to the game interface. ----------

Before the code starts, let me briefly talk about the general framework structure of this instance game, for example.


Startscene. h

/*************************************** * ********************************** Start scenario */ /*************************************** * *******************************/# ifndef _ startscene_h _# define _ startscene_h _/*********************************** *************************************//* introduce the required header file *//******************************** **************************************** /# include "cocos2d. H "# include" cocos -Ext. H "# include" startlayer. H "// start interface /********************************* ***************************************/ /* Add the namespace to be used *//****************************** **************************************** **/using namespace cocos2d; using namespace cocos2d: extension; /***************************************, the first scenario of the game, inherit from ccscene *//*********************************** *** * ********************************/Class startscene: public ccscene {public: /*************************************** * ******************************* // startscene construction * //************************************** * *********************************/startscene (void ); /*************************************** * ******************************* // The analysis structure of * startscene *//************************************* *********** * ***********************/Virtual ~ Startscene (void ); /*************************************** * ******************************* // startscene initialization, if false is returned, initialization fails, true indicates that the operation is successful *//********************************* ***************************************/ virtual bool Init (); /*************************************** * ******************************** // startscene:: Create () method To create scene *//********************************* ***************************************/ create_func (startscene ); protected: Private :}; # endif

Startscene. cpp

# Include "startscene. H "/************************************* * ********************************* // * Constructor, here, we can perform initial value assignment operations on some global variables that require initial value assignment *//******************** **************************************** * ***********/startscene:: startscene (void) {}/************************************* ********************************** // function, when this scenario disappears, the function *//*************************** ******************************* * *************/Startscene ::~ Startscene (void) {}/************************************* * ********************************* // * initialize a function., in this function, we can perform initial placement of some controls on the Interface *//*********************** **************************************** * *******/bool startscene:: Init () {bool Bret = false; /*************************************** * ******************************** // * Because startscene inherits from ccscene, here is a ccscene initialization judgment to determine whether startscene can be successfully created *//************** **************************************** * *****************/If (! Ccscene: Init () {/* If the ccscene initialization fails, it indicates that startscene will not be successfully created, and false */return Bret;} is returned ;} /* here, let's create a log to see if we have completed startscene */cclog ("now, I'm in startscene! ");/* After the ccscene Initialization is successful, we can continue the subsequent operations to design startscene */Bret = true;/* According to the general framework structure I mentioned earlier, add a layer under scene. Here we will add startlayer, which is the main character in the Start scene. The controls displayed on the Start scene interface are from him */cclayer * pstartlayer = startlayer :: create (); addchild (pstartlayer); Return Bret ;}

Startlayer. h

#ifndef _StartLayer_h_#define _StartLayer_h_#include "cocos2d.h"#include "cocos-ext.h"using namespace cocos2d;using namespace cocos2d::extension;class StartLayer:public CCLayer{public:StartLayer(void);virtual ~StartLayer(void);virtual bool init();CREATE_FUNC(StartLayer);virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);protected:private:};#endif

Startlayer. cpp

#include "StartLayer.h"StartLayer::StartLayer(void){}StartLayer::~StartLayer(void){}bool StartLayer::init(){bool bRet = false;if (!CCLayer::init()){return bRet;}bRet = true;CCLog("Now,I'm in StartLayer!");setTouchEnabled(true);return bRet;}void StartLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){}void StartLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent){}void StartLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){}


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.