Cocos2d-x 3.0 game instance learning notes "card tower defense" First Step --- start interface & level selection, cocos2d tower defense
/* Description:
** 1. This game instance is the last game on the cocos2d-x game development journey, which is rewritten and noted down here with 3.0
** 2. I have also asked me about wood. He said: Write it as you like. First, do not copy the code completely. Second, You can note that it is a learning note-Good Guy.
** 3. Here With cocos2d-x 3.0 version rewriting, many places are different, but from the rewriting process also good learning cocos2d-x
*/
* ** All the code corresponding to each step and the resources used are packaged at the end.
* ** To avoid code overhead, the code in each step is marked. At first glance, the code is implemented in the first step to avoid errors and cannot be changed back (if not, Git should be used again ?)
* ** You can follow the design idea (well, the name is too tall. The reality is this step to do) first self-implementation-cocos2d-x is so, the same function has many different implementation methods; first self-toss is quite good.
* ** For the convenience of porting to the mobile phone, android testing is compiled for each step. Because the code can be compiled in win32 many times, compilation will fail, the code is tested.
Start: Create a new project first
Note content:
1. Effect Preview
2. design ideas and precautions
3. Implementation Code & explanation
4. Summary & preview in the next section
I:
Based on the knowledge points in the previous step and the main design ideas in the design ideas, the process from MainScene to the level selection and editing interface is completed first.
Ii. design ideas and attention
MainScene loads a background and has two selection menus.
The Edit button is ignored first;
When the start button is selected, select GameLvlChooseScene for the level.
The Level Selection button is not implemented first.
-(Well, this is a bad design idea .. Bored ...) However, here we will encounter a small coordinate problem-explain it in the code
Iii. Code + resolution
Well, it's actually such a simple code .. Paste it.
MainScene:
# Ifndef _ MainScene _ H __# define _ MainScene _ H __# include "cocos2d. h" USING_NS_CC; class MainScene: public Layer {public: MainScene ();~ MainScene (); CREATE_FUNC (MainScene); virtual bool init (); static Scene * createScene (); private: // ** 1 ** select the menu void chooseMenu (); // void editorScene (Ref * pSender); void startGame (Ref * pSender);};/**/# endif
# Include "MainScene. h" MainScene: MainScene () {} MainScene ::~ MainScene () {} Scene * MainScene: createScene () {auto scene = Scene: create (); auto layer = MainScene: create (); scene-> addChild (layer); return scene;} bool MainScene: init () {auto visibleSize = Director: getInstance ()-> getVisibleSize (); auto BG = Sprite:: create ("game/MainSceneBG.jpg"); BG-> setPosition (visibleSize. width/2, visibleSize. height/2); BG-> setOpacity (200); // sets the transparency, which looks more like the background-> _-> this-> addChild (BG); chooseMenu (); return true;} void MainScene: chooseMenu () {auto visibleSize = Director: getInstance ()-> getVisibleSize (); const char * norImg = "Button/choose_btn_nor.png "; const char * lightImg = "Button/choose_btn_light.png"; // ** 1 ** ----------- Start -------------------------------- auto title = Label: create ("Start", "Arial", 35 ); title-> setPosition (ccp (60, 60); auto startItem = MenuItemImage: create (norImg, lightImg, CC_CALLBACK_1 (MainScene: startGame, this )); startItem-> addChild (title); // ** 1 ** ----------- edit scenario ------------------------------ title = Label: create ("Editor", "Arial", 35 ); title-> setPosition (ccp (60, 60); auto editorItem = MenuItemImage: create (norImg, lightImg, CC_CALLBACK_1 (MainScene: editorScene, this )); editorItem-> addChild (title); // ** 1 ** menu auto menu = CCMenu: create (startItem, editorItem, nullptr ); // ** 1 ** place menu-> alignItemsVerticallyWithPadding (20); menu-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); this-> addChild (menu);} void MainScene: startGame (Ref * pSender) {CCLOG ("startGame"); // auto scene = GameLvlChoose:: createScene (); // ctor: getInstance ()-> replaceScene (scene);} void MainScene: editorScene (Ref * pSender) {CCLOG ("EditorScene ");}
I have no explanation, but pay attention to two questions.
1. A small coordinate problem: In the chooseMenu, we need to add a Label for a single menu item, that is, the text Label such as Start added in the button image, in this way, you will know what the button is. We need to place the Label position in the ccp (60, 60 );
Why? This is because we need to add the Label addChild to the single menu item. The place of son is relative to the parent. If you do not set the position, the default value is ), what does parent mean when it is placed at 0 to 0? Figure:
Then, open the resource image and find that the button image is 130*130, so you should put the Label at (60, 60 ). Please note that this issue will be used later.
2. Concerning the relationship between Scene and Layer, according to the newly created project, there is a Layer in HelloWorld, and the processing method is that HelloWorld is inherited from Layer, it is a Layer. However, in this Layer, we have a createScene method to create and return Scene; In createScene, HelloWorld: create (); then we call the virtual bool init function, create this Layer, add it to scene, and return scene. The MainScene here is relatively simple. This is also the case. For the latter part, there are multiple layers in a Scene, so they can be separated.
Bytes -------------------------------------------------------------------------------------------------
Then modify scene in AppDelegate. cpp.
Bool AppDelegate: applicationDidFinishLaunching () {// initialize director auto director = Director: getInstance (); auto glview = director-> getOpenGLView (); if (! Glview) {glview = GLView: create ("My Game"); glview-> setFrameSize (800,450); <span style = "color: # ff0000; ">// ------------------ under Win32, set ---------------------- </span> ctor-> setOpenGLView (glview);} glview-> setDesignResolutionSize (800,450, ResolutionPolicy: SHOW_ALL ); // screen adaptive /*.......... omitted ..................... */return true ;}
Okay. In the above step, you can test it in F5. After the button, you can see the corresponding callback function output.
Then join the level and select Scene.
# Ifndef _ GameLvlChoose _ H __# define _ GameLvlChoose _ H __# include "cocos2d. h" USING_NS_CC; class GameLvlChoose: public Layer {public: GameLvlChoose ();~ GameLvlChoose (); virtual bool init (); CREATE_FUNC (GameLvlChoose); static Scene * createScene (); private: // -- 1 -- add a menu in init, here is the callback function void level_1 (Ref * pSender); void level_2 (Ref * pSender); void level_3 (Ref * pSender); // -- 1 -- createLabel based on different levels, menu Label * createLevelLab (const char * sLvl); int _ level ;};/**/# endif
. Cpp
# Include "GameLvlChoose. h" GameLvlChoose: GameLvlChoose () {_ level = 1;} GameLvlChoose ::~ GameLvlChoose () {} Scene * GameLvlChoose: createScene () {Scene * scene = Scene: create (); auto layer = GameLvlChoose: create (); scene-> addChild (layer); return scene;} bool GameLvlChoose: init () {auto visibleSize = Director: getInstance ()-> getVisibleSize (); // ** 1 ** -------- create a level selection. Then, the menu is saved const char * norImg = "Button/choose_btn_nor.png"; const char * lightImg = "Button/choose_btn_light.png "; // Lv_1auto level_0000item = MenuItemImage: create (norImg, lightImg, CC_CALLBACK_1 (usage: level_1, this); level_0000item-> addChild (createLevelLab ("1 ")); // Lv_2auto level_2_Item = MenuItemImage: create (norImg, lightImg, CC_CALLBACK_1 (latency: level_2, this); level_2_Item-> addChild (createLevelLab ("2 ")); // Lv_3auto level_3_Item = MenuItemImage: create (norImg, lightImg, CC_CALLBACK_1 (latency: level_3, this); level_3_Item-> addChild (createLevelLab ("3 ")); // ** 1 ** menu auto menu = CCMenu: create (level_1_Item, level_2_Item, level_3_Item, nullptr ); // ** 1 ** place menu-> alignItemsHorizontallyWithPadding (20); menu-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); this-> addChild (menu); return true;} Label * GameLvlChoose: createLevelLab (const char * sLvl) {auto level_lab = Label :: create (sLvl, "Arial", 60); level_lab-> setColor (Color3B: RED); level_lab-> setPosition (ccp (60, 60); return level_lab ;} void GameLvlChoose: level_1 (CCObject * pSender) {CCLOG ("111111"); _ level = 1;} void GameLvlChoose: level_2 (CCObject * pSender) {CCLOG ("222222"); _ level = 2;} void GameLvlChoose: level_3 (CCObject * pSender) {CCLOG ("333333"); _ level = 3 ;}
Later, you can switch the scenario in the StartGame function in MainScene.
Iv. Summary & next Preview
Conclusion: Nothing to say...
Next time, we will start to edit Scene. We can freely edit the monster walking routes at each level and the positions where the hero can be placed.
------------------------------------------------------
Code & Resources
------------------------------------------------------
Personal ignorance, welcome to correction and discussion -------
Cocos2d-x 30 game engine, what language to edit the game
Supports c ++, lua, and javascript. The underlying layer is implemented in c ++, and the upper layer can be bound to two other scripting languages.
What are the best places to study Cocos2d-x game training school? It is also responsible for employment
Learning Cocos2d-x game professional good Oh, looking for large formal, the tuition is cheap but can't learn anything don't money is useless