Cocos2d-x3.0 beginner C ++ tower guard instance game
/* 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.
Note content:
1. Design Ideas
2. Code &
3. Next Content preview
4. Download the source code and resources
I. design ideas:
1. Start to create a game scene. You can add getbacklayer to scene.
2. According to the initial general design flowchart, you need a most important game layer, and then add the hero and the monster manager. Here, we only get the hero manager.
3. The hero manager inherits from the layer? In this step, we load the towerpos that we have compiled based on the current level. towerpos is the gunshots displayed by the Genie. We need to click the turret to add heroes to it. Therefore, the touch mechanism is required.
4. There are many content pages ......
Ii. Code &
After the editing is completed, the game interface will be officially started. gamescene, which is extracted separately and added to getbacklayer first. Among them, gamescene is createscene based on the level selected by the level.
class GameScene{public:static Scene* createScene(int level);};. Cpp
Scene* GameScene::createScene(int level){Scene* scene = NULL;scene = Scene::create();auto getBackLayer = GetBackLayer::create();scene->addChild(getBackLayer);return scene;}You can switch the scenario in the callback function in gamelvlchoose.
Bytes ---------------------------------------------------------------------------------------
At this point, we will prepare the last layer, which is also the most important maplayer in the game. In the design concept, maplayer carries two sons, namely the hero manager and the monster manager;
The whole game is separated from each other in such a hierarchy. It is also easy to expand. I can't help but sigh: the wooden main journey is not covered !!!
Maplayer. h
Class maplayer: Public layer {public: maplayer ();~ Maplayer (); static maplayer * Create (INT level); bool Init (INT level); Private: // ** 5 ** int _ curlevel; // ** 5 ** load the level map void preload ();};. Cpp
MapLayer::MapLayer(){_curLevel = 1;}MapLayer::~MapLayer(){}MapLayer* MapLayer::create(int level){MapLayer* mapLayer = new MapLayer();if(mapLayer && mapLayer->init(level)){mapLayer->autorelease();}else{CC_SAFE_DELETE(mapLayer);}return mapLayer;}bool MapLayer::init(int level){_curLevel = level;preLoad();return true;}void MapLayer::preLoad(){auto visibleSize = Director::getInstance()->getVisibleSize();//add map__String* sBG = __String::createWithFormat("game/level_%d.jpg",_curLevel);Sprite* map = Sprite::create(sBG->getCString());map->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));this->addChild(map);}Then add the following in gamescene:
auto mapLayer = MapLayer::create(level);mapLayer->setTag(Tag_Map); scene->addChild(mapLayer);
In order not to block the back button, we should add it before getbacklayer, and then tag_map is a macro-defined tag, which will be used later. As for macro-defined values, it doesn't matter if you just take them.
Bytes ---------------------------------------------------------------------------------------
Here, let's talk about monsters. Let's look at the hero manager first. Heromanager; first note that this manager is inherited from the layer. Why?
Heromanager's function: it loads the towerpos we prepared in advance according to the current level, that is, the gun station coordinates. Then, we will use a gun station genie to display these coordinates; the purpose of the bastion host is to allow you to add a hero after clicking the bastion host. You need a touch mechanism, so the manager inherited from the layer is used here.
Mount:. h
class HeroManager : public Layer{public:HeroManager(); ~HeroManager();static HeroManager* createWithLevel(int curLevel);bool initWithLevel(int curLevel);private://**5**Vector<PosBase*> m_towerPosList;//--------------------------------------------------//**5**void createTowerPos(int curLevel);};. Cpp
Heromanager: heromanager () {} heromanager ::~ Heromanager () {} heromanager * heromanager: createwithlevel (INT curlevel) {heromanager * heromgr = new heromanager (); If (heromgr & heromgr-> initwithlevel (curlevel )) {heromgr-> autorelease ();} else {cc_safe_delete (heromgr);} return heromgr;} bool heromanager: initwithlevel (INT curlevel) {// ** 5 ** createtowerpos (curlevel); // --------------------- touch event ----------------------------------- auto listener = eventlistenertouchone Byone: Create (); listener-> setswallowtouches (true); listener-> ontouchbegan = [] (touch * touch, event * event) {return true ;}; listener-> ontouchmoved = [] (touch * touch, event * event) {}; listener-> ontouchended = [=] (touch * touch, event * event) {auto touchpos = touch-> getlocationinview (); Auto Pos = Director: getinstance ()-> converttoui (touchpos); cclog ("Touch POS. X is % F, POS. Y is % F! ", POS. x, POS. y) ;}; _ eventdispatcher-> addeventlistenerwithscenegraphpriority (listener, this); // returns true;} void heromanager: createtowerpos (INT curlevel) {// ** 5 ** _ string * towerpospath = _ string: createwithformat ("game/towerpos_level _ % d. plist ", curlevel); posloadutil: getinstance ()-> loadposwithfile (m_towerposlist, entowerpos, towerpospath-> getcstring (), this, 1, false ); // ---- can be changed to true}Here, only the coordinate points are loaded. What effect can't be seen ;?? Nothing, no fort genie?
It's not as easy as simply using genie to show the fort... Solve this problem later
Here, to see the effect, change the debugging parameter in the createtowerpos function in the Manager to true, and then set the transparency of the map in the map layer;
Then, a heromanager * _ heromgr; member is assigned to the maplayer. In the init function:
preLoad();_heroMgr = HeroManager::createWithLevel(_curLevel);this->addChild(_heroMgr);
The running result diagram is as follows:
Iii. Next content
What is the only thing that counts? What about the bastion hosts displayed by the genie? How can I add a hero when I click on the bastion host?
4. Source Code & Resources
----------------------------------
Source code & Resources
---------------------------------
Personal ignorance. Thank you for your correction and discussion.
Cocos2d-x 3.0 game instance learning notes "tower guard" Step 5 --- start to build a game interface