Cocos2dx learning report 2

Source: Internet
Author: User

Cocos2dx learning report 2
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 <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> /************************************* * ********************************** // 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, inheriting CC Scene *//************************************ * **********************************/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 starting scene. The controls displayed on the starting scene interface all come from him * // get the screen form size, the CCSize Winsize = CCDirector: shareddire()-> getWinSize (); CCLayer * pstartLayer = StartLayer: create (); pstartLayer-> setPosition (ccp (Winsize. width/2, Winsize. height/2); // set the position for the Layer to prepare for future processing addChild (pstartLayer); return bRet ;}


StartLayer. h

/*************************************** * ********************************** // The Game start interface, it is the content display of the game scenario, that is, the control displayed in the game start scenario is placed in it, events are triggered and processed in this process *//************************** **************************************** * *****/# ifndef _ StartLayer_h _ # define _ StartLayer_h _ // header file introduction # include "cocos2d. h "# include" cocos-ext.h "// namespace using namespace cocos2d; using namespace cocos2d: extension; /****************************** **************************************** ** // * Game start interface, inherited from CCLayer *//********************************** * ***********************************/class startLayer: public CCLayer {public: StartLayer (void); // construct virtual ~ StartLayer (void); // destructor virtual bool init (); // initialize CREATE_FUNC (StartLayer ); /*************************************** * ******************************** // * screen touch events, here I am writing multi-touch events, including Began (start), Moved (MOVE), Ended (end), and Canceled (cancel). I am not very familiar with this event, so I did not write the code below) *//************************************* * ********************************/virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent); // start with virtual void ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent); // touch mobile virtual void Merge (CCSet * pTouches, CCEvent * pEvent ); // touch the end protected: private: /*************************************** * ********************************** click the button to jump to the game scenario callback, in this jump, we first jump to GameScene, and then the GameLayer in GameScene shows the content of GameScene, in this way, the feeling of clicking the button to start the game *//**************************** **************************************** * ***/void gotoGame (CCObject * pSender );}; # 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! "); // This page is written with multi-touch, so you need to enable the following code to enable the setTouchEnabled (true) touch event of the corresponding page ); // define a text button to jump to CCMenuItemLabel * pButton = CCMenuItemLabel: create (CCLabelTTF: create ("Start Game", "Arial", 25), this, menu_selector (StartLayer: gotoGame )); /*************************************** * *********************************** about the button, here, we use Menu + MenuItem to implement a button. MenuItem can be added to the Layer independently. If this is done, the MenuItem callback event will not be responded, and the tower will be like a CCSprite. However, if you add MenuItem to the Menu and add the Menu to the Layer, the MenuItem callback can be responded. Note that you cannot add MenuItem to the layer after adding MenuItem to the Menu, because this will cause the Parent to be not unique and an error will be reported. *//************************************* * *********************************/CCMenu * pMenu = CCMenu:: create (pButton, NULL); // Add a MenuItem to the menu. You can also create an empty Menu first and then add Child (menuitem) pMenu-> setAnchorPoint (ccp (); // set the Menu anchorpoint to () pMenu-> setPosition (ccp )); // set the coordinates of the Menu to (0, 0) addChild (pMenu); // Add the Menu to the layer return bRet;} void StartLayer: gotoGame (CCObject * pSender) {// To Do // jump To the game scenario CCScene * pgameScene = CCScene: create (); CCDirector: sharedDirector ()-> replaceScene (pgameScene);} void StartLayer :: vertex (CCSet * pTouches, CCEvent * pEvent) {} void StartLayer: ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent) {} void StartLayer: vertex (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.