1. Check off
15 per page, 45 total, 5 per line. These can all be changed. Source code based on Cocos2d 3.3. 2. Principle of Implementation Anyway, there is the source of the less talk about the point. I look at the implementation of the Cocos2d menu, is a layer, and then add a lot of MenuItem inside, if it is the default way to add, this few rows of a few columns of difficult to engage, so we just need to create an empty menu, and then in our way one by one add MenuItem can.
[CPP]View Plaincopy
- 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);
- }
The essence is a for loop, adding a MenuItem 3. Custom Menuitemsprite we have 3 states for each of our chosen customs, which have been completed, will be completed, and locked. Assuming that the user has completed the 5th level, then the first 5 off is completed, the 6th is going to be completed, the back of all is locked. So here's a definition of an enumeration
[CPP]View Plaincopy
- typedef enum{
- Kalreadypass,
- Knotpassyet,
- Klocklevel
- }levelitemtype;
[CPP]View Plaincopy
- Levelselectitem::levelselectitem (int): _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 can decide what you want based on the number of levels. COCOS2D supports 3 image styles by default, Normal, selected, disabled, so you only need to set the back level MenuItem to be unavailable. or directly look at the source code, do not rip, see the source is relatively clear.
COCOS2D Game Select the interface with the source code