How to realize the selection Interface of fighting characters in cocos2dx3.2
This article is to share with you about cocos2dx3.2 how to achieve the fighting character selection interface, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
First, take a look at the effect picture of the final implementation:
Create a ChooseHero.h to select a scene with characters
# ifndef _ _ CHOOSE_HERO_H__#define _ _ CHOOSE_HERO_H__#include "cocos2d.h" # include "ui/CocosGUI.h" # include "cocos-ext.h" # include "cocostudio/CocoStudio.h" using namespace cocos2d::ui;USING_NS_CC;class ChooseHero: public Layer {public:static Scene * createScene (); virtual bool init (); void OnGameStartPage (Object* pSender, Widget::TouchEventType type); / / create character void textFieldEvent (Object* pSender, Event type) / / void OnBackToLoginPage (Object * pSender, Event type); / return login interface void OnRoleSelect (CCObject * pSender, Widget::TouchEventType type); / / role selection interface void OnRoleSelect2 (CCObject * pSender, Widget::TouchEventType type); / / role selection interface void OnRoleSelect3 (CCObject * pSender, Widget::TouchEventType type); / / role selection interface bool OnRoleSelectMy (Touch * touch, Event * event); CREATE_FUNC (ChooseHero); protected:// tag enum {enTagStudioMainUiLayer,enTagRole,}; intm_nRoleID;// role ID 1. Naruto 2. Sakura 3. Sasuke int btnIndex2;Button* pBtn_Role; Button* pBtn_Role2;Button* pBtn_Role3;ImageView * pSelect_Img; ImageView * pSelect_Img2;ImageView * pSelect_Img3;}; # endif
The init implementation is as follows:
Bool ChooseHero::init () {if (! Layer::init ()) {return false;} m_nRoleID = 1CreateNinja.json / create the main scene UI layer auto pCreateNinja = GUIReader::getInstance ()-> widgetFromJsonFile ("CreateNinja.json"); addChild (pCreateNinja, 100th, enTagStudioMainUiLayer); pCreateNinja- > setName ("CreateNinjaLayer"); pCreateNinja- > setSize (getContentSize ()); ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/xiaoyin.ExportJson"); ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/NewProject.ExportJson") ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/minren1.ExportJson"); / get the start button Button * pBtn_Create_Team = dynamic_cast (pCreateNinja- > getChildByName ("MainUi")-> getChildByName ("Btn_Create_Team")); pBtn_Create_Team- > addTouchEventListener (this, toucheventselector (ChooseHero::OnGameStartPage)); / get the input box TextField* pTextField = dynamic_cast (pCreateNinja- > getChildByName ("MainUi")-> getChildByName ("Team_Name_TextField")); / / CC_BREAK_IF (pTextField = = NULL) PTextField- > setTouchEnabled (true); / / pTextField- > addEventListenerTextField (this, textfieldeventselector (CNFTeamCreateLayer::textFieldEvent)); pTextField- > setText (""); / / get the character button pBtn_Role = static_cast (pCreateNinja- > getChildByName ("MainUi")-> getChildByName (String::createWithFormat ("Btn_Char_%d", 0 + 1)-> getCString ()); pBtn_Role2 = static_cast (pCreateNinja- > getChildByName ("MainUi")-> getChildByName (String::createWithFormat ("Btn_Char_%d", 1 + 1)-> getCString () PBtn_Role3 = static_cast (pCreateNinja- > getChildByName ("MainUi")-> getChildByName (String::createWithFormat ("Btn_Char_%d", 2 + 1)-> getCString ()); pSelect_Img = static_cast (pBtn_Role- > getChildByName ("Select_Img")); pSelect_Img2 = static_cast (pBtn_Role2- > getChildByName ("Select_Img")); pSelect_Img3 = static_cast (pBtn_Role3- > getChildByName ("Select_Img")); pBtn_Role- > addTouchEventListener (this, toucheventselector (ChooseHero::OnRoleSelect)) PBtn_Role2- > addTouchEventListener (this, toucheventselector (ChooseHero::OnRoleSelect2); pBtn_Role3- > addTouchEventListener (this, toucheventselector (ChooseHero::OnRoleSelect3)); pSelect_Img- > setVisible (true); ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/xiaoyin.ExportJson"); ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/NewProject.ExportJson"); ArmatureDataManager::getInstance ()-> addArmatureFileInfo ("armature/minren1.ExportJson"); char cName [3] [256] = {"minren1", "xiaoyin", "NewProject"} Armature * pRole = Armature::create (cName [0]); pRole- > setPosition (ccp (640 / 2210)); pRole- > setAnchorPoint (pRole- > getAnchorPoint (). X, 0); pRole- > getAnimation ()-> play ("hold"); addChild (pRole, 100000, enTagRole); return true;}
The callback method is implemented as follows:
Void ChooseHero::OnRoleSelect (CCObject * pSender, Widget::TouchEventType type) {if (pBtn_Role = = pSender) {pSelect_Img2- > setVisible (false); pSelect_Img3- > setVisible (false); pSelect_Img- > setVisible (true); m_nRoleID = 1 setremoveChildByTag (enTagRole); char cName [3] [256] = {"minren1", "xiaoyin", "NewProject"}; Armature * pRole = Armature::create (cName [0]); pRole- > setPosition (ccp (640 / 2210) PRole- > setAnchorPoint (pRole- > getAnchorPoint (). X, 0); pRole- > getAnimation ()-> play ("hold"); addChild (pRole, 100000, enTagRole);} else {pSelect_Img- > setVisible (false);}} above is how cocos2dx3.2 implements the fighting character selection interface. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.