Cocos creator often use buttons in the development of a game, especially when a large number of buttons are used, the use of arrays to manage these buttons becomes more versatile. I walked around the official example, as if I hadn't found the small content (or there was, but I didn't find it), so I added the following.
Typical problem preview
such as the display of the interface (the picture is a small example of me, not yet mature, and so later feel can be shared after the public):
Note that, in hierarchical management, I used a parent node controlroot to contain two button nodes (there may be many buttons in practice).
Create an Action Script component
The code (ZXZLEVELSELECT.JS) is as follows:
cc. Class ({extends:cc.Component, properties: {but: {default: [], type: [CC]. Button],//Type is also written as an array, improving code readability}}, Touchbutton (event, customeventdata) {var node = Event.target; Switch (node.name) {case ' btnstart ': {cc.director.loadScene (' zxzballscene '); } break; Case ' Btnback ': {cc.director.loadScene (' zxzwelcome '); } break; }//switch (Customeventdata) {////switch (button.name) {//Case ' 0 '://{ Cc.director.loadScene (' Zxzballscene '); }//break; Case ' 1 '://{//Cc.director.loadScene (' zxzwelcome '); }//break; // } }});
Note the following points in your code:
1, Function Touchbutton (event, Customeventdata) is the handler function that we define the button;
2, in this function, we can at least use the two ways shown above to distinguish between buttons, The first way is Event.target.name (Event.target corresponds to the corresponding button component), and the name value is the name of the button we see in the Hierarchy Manager, and the second way is to use the second parameter in the handler function Customeventdata, in the scene design I Set the Customeventdata values of the two buttons to 0 and 1 respectively (you can, of course, set them to a more intuitive name, such as strings). Notice that the second half of the code uses a comment, which is exactly how the button is differentiated.
Associate Script Component and Set button handler function
The small scene file named Zxzlevelselection.fire, I associate the above script zxzlevelselect.js on the topmost canvas node of hierarchy management, and bind the above two button components as shown in:
Then, select the two button components, each associated with the corresponding handler function, as shown (only one can be given):
Please note: No matter how many buttons, you have to follow the idea of association.
Summary
In this paper, the use of the button component array in Cocos Creator programming is supplemented, please remind me, thank you very much.
Cocos use of button component arrays in creator