Cocos2d-x-3.0 alpha1 with C ++ 11 Exercise 7: dart ninja, add new Scene and do some icing on the cake

Source: Internet
Author: User

A simple game has basically been completed, but we can add some modifications to it to make it more professional. This time, we will add new scenarios. When You get rid of a certain number of monsters, "You Win" is displayed on the screen, and "You Lose" is displayed when a monster escapes from the left side of the screen ". 1. The interface definition adds a GameOverScene to the xcode project. cpp file, which will automatically add a GameOverScene. h. The initial h file does not reference cocos2d. h. modify it as follows: # ifndef _ cocos2dx3sample _ GameOverScene __# define _ cocos2dx3sample _ GameOverScene _ # include "cocos2d. h "USING_NS_CC; class GameOverScene: public cocos2d: LayerColor {public: bool initWithWon (int won); static cocos2d: Scene * sceneWithWon (bool won ); CREATE_FUNC (GameOverScene) ;};# endif/* defined (_ cocos2dx3 Sample _ GameOverScene _) */macro CREATE_FUNC is used to create an automatic release of the current object. The init method is called. For the source code, see # define CREATE_FUNC (_ TYPE __) \ static _ TYPE _ * create () \ {\ _ TYPE _ * pRet = new _ TYPE __(); \ if (pRet & pRet-> init () \ {\ pRet-> autorelease (); \ return pRet ;\}\ else \ {\ delete pRet; \ pRet = NULL; \ return NULL; \}\} CREATE_FUNC can be used as the standard and added to each Scene. The create method will be used in sceneWithWon. 2. Implement GameOverScene. cpp. The Code is as follows: + View Code defines a gameOverDone function in the original tutorial. Here it is replaced by a lambda expression, which is intended for practice. All objects passed to ctor: getInstance ()-> replaceScene are static methods of scene *. All objects created by these methods are automatically released. Compile & run, effect: So far, darts ninja with cocos2d-x 3.0 rewrite has been completed. 3. The preceding code for the chat * variable warning has a warning: 1 Conversion from string literal to 'Char * 'is deprecated. This warning falls on the message assignment. Because in c ++, the string literal value is a constant, you can modify the chat * type to const chat :... const char * message; if (won) {message = "You win" ;}else {message = "You lose ";}... in C, there is no string data type, but a character array is used to save the string. The C string is actually an array of characters ending with null ('\ 0'). The null Character indicates the end of the string. Note that only the character array ending with null characters is the C string, otherwise it is only a general C character array. The C string can be initialized with the "=" sign, but cannot be assigned a value to the C string later. To operate a C string, use the string processing function defined in the "string" file. For example: // string initialization char a [11] = "huanying"; // string value assignment strcpy (a, "nihao") // obtain the string length, strlen (a); printf ("% s", a) excluding '\ 0'; in C, you can also use character pointers to access a string, the character Pointer Points to the address of the first element that stores the string array for access. 12 char * a = "nihao"; printf ("% s", a); in C ++, the string is encapsulated into a data type string, you can directly declare variables and assign values to strings. The following is the difference between string in C string and C ++: the header file name <string> or <string. h> <string> or <string. h> Why does the header file need to be used to use a string function? How does one declare char name [20]; string name; and how to initialize char name [20] = "nihao "; string name = "nihao"; do I have to declare the string length? Is it a null character? Is it true that how to assign a value to a string strcpy (name, "John"); name = "John"; other advantages are faster and easier to use. Is it better to assign a string that is longer than the existing character? It cannot be 4. There are two types of c-series strings to release: one is a character array, for example, chat message [] = "You win". The other is a character pointer, for example, chat * message = "You lose" for character pointers, use the following method to release: delete [] message; // delete [] message, not delete messagemessage = NULL; for character arrays, delete cannot be used for release. As a local variable of the function, it is automatically released when the function exits. The memory occupied by a C/C ++ compiled program is divided into the following parts: 1. the stack zone (stack) is automatically allocated and released by the compiler to store the function parameter values, the value of a local variable. The operation method is similar to the stack in the data structure. 2. heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list. 3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The program is released by the system after it is completed. 4. Text Constant Area-constant strings are placed here. After the program ends, the system releases 5. The program code area-stores the binary code of the function body. The character pointer * message in the preceding figure. The memory is allocated in the stack area. After the function exits, it is automatically released.

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.