Source code and cocos2d source code are included in the cocos2d game selection interface.
1. Select a parameter
15 entries per page, 45 entries in total, and 5 entries per line. These can be changed. The source code is Cocos2d 3.3.
2. Implementation Principle
If you have the source code, let's skip it. I can see that the implementation of the cocos2d Menu is a Layer, and many menuitems are added in it. If it is used as the default addition method, it is difficult to implement such several rows and columns, therefore, we only need to create an empty Menu and add MenuItem one by one in our way.
void LevelSelectContent::initAllLevels(int page){ this->removeAllChildren(); const int eachPageItemCount = g_EachPageCount; _levelMenu = Menu::create(); this->addChild(_levelMenu); int const constStartPositionX = VISIBLE_WIDTH * 0.5 - levelMarginX * (g_EachLineCount / 2 - 0.5); int startPositionX = constStartPositionX; int topStartPositionY = VISIBLE_HEIGHT * 0.5 + levelMarginY * (g_EachPageCount / g_EachLineCount / 2 - 0.5); for(int i = 0 + page * eachPageItemCount; i < eachPageItemCount + page * eachPageItemCount; i++){ if(i < g_maxLevel){ int lineCount = g_EachLineCount; if(i!= (0 + page * eachPageItemCount) && i % lineCount == 0){ startPositionX = constStartPositionX; topStartPositionY = topStartPositionY - levelMarginY; } int levelCount = i + 1; auto oneLevelItem = LevelSelectItem::create(levelCount); _levelMenu->addChild(oneLevelItem); oneLevelItem->setPosition(startPositionX, topStartPositionY); startPositionX += levelMarginX; } } _levelMenu->setPosition(0, 0); _levelMenu->setOpacity(0); auto fadeAction = FadeIn::create(0.5); _levelMenu->runAction(fadeAction);}
In essence, it is a for loop, adding a MenuItem
3. Customize MenuItemSprite
Each of our selections has three statuses: completed, to be completed, and locked. Assume that the user has completed the 5th off, the first 5 off are complete, the 6th off is to be completed, and the rest are locked. Therefore, an enumeration is defined here.
typedef enum{ kAlreadyPass, kNotPassYet, kLockLevel}LevelItemType;
LevelSelectItem::LevelSelectItem(int level):_level(level),_type(kLockLevel){ if(level > (g_passLevelCount + 1)){ this->setEnabled(false); _type = kLockLevel; }else if(level == (g_passLevelCount + 1)){ this->setEnabled(true); _type = kNotPassYet; }else if(level < (g_passLevelCount + 1)){ this->setEnabled(true); _type = kAlreadyPass; }}
You only need to determine the number of levels. By default, cocos2d supports three image styles: normal, selected, and disabled. In this way, you only need to set the levels menuitem to unavailable.
Let's look at the source code directly. If you don't need to talk about it, you can see the source code clearly.
4. Miscellaneous time
1. As a programmer, I personally think it is best not to waste time on the selection of the interface, sliding mode, click mode can be achieved, click more easily, how simple implementation, how to achieve. 2. This kind of selection of the most classic game is the Angry Birds, but now you have worked hard to do this kind of selection of the game, it is likely that the sea of stone, we recommend not to do this kind of selection of the game, remember!
The following game uses the selection interface above. This game is original and has a hard time creating it. It has experienced the pain and joy of making an original game. We recommend you download and play it. I usually use cocos2d lua for future games, so this may be the last cocos2d c ++ example. IPhone download: https://itunes.apple.com/cn/app/findball/id951664460? L = zh & mt = 8 Android download: http://toycloud.qiniudn.com/FindBall.apk or scan QR code to download the game to play.
4. Download the project source code
Http://www.waitingfy.com /? Attachment_id = 1472
Http://www.waitingfy.com/archives/1476