Analysis of Cocos2d-x horizontal version of the ACT mobile game source code 6, the creation of the team Interface
. H header file code
# Ifndef _ NF_TEAM_CREATE_SCENE_HEAD _ # define _ NF_TEAM_CREATE_SCENE_HEAD _ # include ".. /publicdef/PublicDef. h "/************************************* *********************************** // team Layer *//*********************************** * **********************************/class CNFTeamCreateLayer: public CCLayer {protected: // tag enum {enTagStudioMainUiLayer, enTagRole,}; intm_nRoleID; // role ID1. Naruto 2. sakura 3. sas public: static variables * CreateLayer (); static CCScene * scene (); void OnGameStartPage (CCObject * pSender, TouchEventType); // create the void textFieldEvent (CCObject * pSender, textFiledEventType type); void OnBackToLoginPage (CCObject * pSender, TouchEventType); // return to the logon interface void OnRoleSelect (CCObject * pSender, TouchEventType type); // role selection interface protected: virtual bool init () ;};# endif
. Cpp Code
# Include "NFRoleSelectLayer. h "# include" NFTownScene. h "# include" NFLoginScene. h "# include" NFCreateTeamLayer. h "# include" datamanager/NFServerDataManager. h "/************************************* *********************************** // team Layer *//*********************************** * **********************************/CCScene * CNFTeamCreateLayer:: scene () {do {CCScene * pScene = CCScene: create (); CC_BREAK_IF (p Scene = NULL); CNFTeamCreateLayer * pLayer = CNFTeamCreateLayer: CreateLayer (); CC_BREAK_IF (pLayer = NULL); pScene-> addChild (pLayer); return pScene ;} while (false); CCLog ("Fun CNFTeamCreateLayer: scene Error! "); Return NULL;} CNFTeamCreateLayer * CNFTeamCreateLayer: CreateLayer () {CNFTeamCreateLayer * pRet = new CNFTeamCreateLayer (); if (pRet & pRet-> init ()) {pRet-> autorelease (); return pRet;} CCLog ("Fun CNFTeamCreateLayer: CreateLayer Error! "); Delete pRet; pRet = NULL; return NULL;} bool CNFTeamCreateLayer: init () {do {// initialize the parent class CC_BREAK_IF (CCLayer: init () = false); m_nRoleID = 1; // create the UI Layer UILayer * pUiLayer = UILayer: create (); CC_BREAK_IF (pUiLayer = NULL) of the main scenario ); // ------------------- Add the main scenario UI ---------------------------- UILayout * pCreateNinja = dynamic_cast
(GUIReader: shareReader ()-> widgetFromJsonFile ("CreateNinja. json "); CC_BREAK_IF (pCreateNinja = NULL); pUiLayer-> addWidget (pCreateNinja); pCreateNinja-> setName (" CreateNinjaLayer "); pCreateNinja-> setSize (getContentSize (); CCArmatureDataManager: sharedArmatureDataManager ()-> addArmatureFileInfo ("armature/xiaoyin. exportJson "); CCArmatureDataManager: sharedArmatureDataManager ()-> addArmatureFileInfo (" armature/NewProject. exportJson "); CCArmatureDataManager: sharedArmatureDataManager ()-> addArmatureFileInfo (" armature/minren1.ExportJson "); // obtain the return button UIButton * pBtn_Back = dynamic_cast
(Response-> getChildByName ("MainUi")-> getChildByName ("Btn_Return"); Response (pBtn_Back = NULL); pBtn_Back-> addTouchEventListener (this, toucheventselector (response :: onBackToLoginPage); // obtain the start button UIButton * pBtn_Create_Team = dynamic_cast
(Response-> getChildByName ("MainUi")-> getChildByName ("response"); Response (pBtn_Create_Team = NULL); pBtn_Create_Team-> addTouchEventListener (this, toucheventselector (response :: onGameStartPage); // obtain the input box UITextField * 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 (""); for (int I = 0; I <3; I ++) {// obtain the character button UIButton * pBtn_Role = dynamic_cast
(PCreateNinja-> getChildByName ("MainUi")-> getChildByName (CCString: createWithFormat ("Btn_Char _ % d", I + 1)-> getCString ())); CC_BREAK_IF (pBtn_Role = NULL); pBtn_Role-> addTouchEventListener (this, toucheventselector (CNFTeamCreateLayer: OnRoleSelect); if (I = 0) {// obtain the UIImageView * pSelect_Img = dynamic_cast
(PBtn_Role-> getChildByName ("Select_Img"); CC_BREAK_IF (pSelect_Img = NULL); pSelect_Img-> setVisible (true) ;}} CCArmatureDataManager: callback () -> addArmatureFileInfo ("armature/xiaoyin. exportJson "); CCArmatureDataManager: sharedArmatureDataManager ()-> addArmatureFileInfo (" armature/NewProject. exportJson "); CCArmatureDataManager: sharedArmatureDataManager ()-> addArmatureFileInfo (" armature/minre N1.ExportJson "); char cName [3] [256] = {" minren1 "," xiaoyin "," NewProject "}; CCArmature * pRole = CCArmature :: create (cName [0]); CC_BREAK_IF (pRole = NULL); pRole-> setPosition (ccp (SCREEN_WIDTH/2,210 )); pRole-> setAnchorPoint (ccp (pRole-> getAnchorPoint (). x, 0); pRole-> getAnimation ()-> play ("hold",-1,-1,-1, 10000); addChild (pRole, enZOrderMid, enTagRole ); addChild (pUiLayer, enZOrderBack, enTagStudioMainUiLayer); return true;} While (false); CCLog ("Fun CNFTeamCreateLayer: init Error! "); Return false;} void CNFTeamCreateLayer: OnGameStartPage (CCObject * pSender, TouchEventType) {do {switch (type) {case TOUCH_EVENT_ENDED: {if (m_nRoleID = 0) return; UILayer * pMainUiLayer = dynamic_cast
(GetChildByTag (enTagStudioMainUiLayer); CC_BREAK_IF (pMainUiLayer = NULL); UILayout * pCreateNinja = dynamic_cast
(PMainUiLayer-> getWidgetByName ("CreateNinjaLayer"); CC_BREAK_IF (pCreateNinja = NULL); // obtain the UITextField * pTextField = dynamic_cast
(PCreateNinja-> getChildByName ("MainUi")-> getChildByName ("Team_Name_TextField"); struct (pTextField = NULL); if (strcmp (pTextField-> getStringValue (), "") = 0) return; // read role information CNFServerRoleSelectInfoVec RoleSelectInfoVec; CNFServerDataManager: SharedData ()-> GetRoleSelectInfo (RoleSelectInfoVec); Role: iterator itRole = RoleSelectInfoVec. begin (); for (; itRole! = RoleSelectInfoVec. end (); itRole ++) {if (itRole-> nRoleID = m_nRoleID) {itRole-> nState = 0 ;}} CNFServerDataManager: SharedData () -> SetRoleSelectInfo (RoleSelectInfoVec); // CCScene * pScene = CNFRoleSelectLayer: scene (); CC_BREAK_IF (pScene = NULL); CCDirector: shareddire () -> replaceScene (CCTransitionFade: create (1.f, pScene);} break; default: break;} return;} while (false); CCLog ("CNFTeamCreateLayer :: onGameStartPage Error ");} void identifier: textFieldEvent (CCObject * pSender, TextFiledEventType) {} void identifier: OnBackToLoginPage (CCObject * pSender, TouchEventType type) {do {// jump to the login interface CCScene * pScene = CNFLoginScene: scene (); locate (pScene = NULL); CCDirector: sharedDirector ()-> replaceScene (CCTransitionFade :: create (1.f, pScene); return;} while (false); CCLog ("failed: OnBackToLoginPage Error");} void CNFTeamCreateLayer: OnRoleSelect (CCObject * pSender, TouchEventType type) {do {UILayer * pMainUiLayer = dynamic_cast
(GetChildByTag (enTagStudioMainUiLayer); CC_BREAK_IF (pMainUiLayer = NULL); UILayout * pCreateNinja = dynamic_cast
(PMainUiLayer-> getWidgetByName ("CreateNinjaLayer"); CC_BREAK_IF (pCreateNinja = NULL); for (int I = 0; I <3; I ++) {// obtain the character button UIButton * pBtn_Role = dynamic_cast
(PCreateNinja-> getChildByName ("MainUi")-> getChildByName (CCString: createWithFormat ("Btn_Char _ % d", I + 1)-> getCString ())); CC_BREAK_IF (pBtn_Role = NULL); // obtain the UIImageView * pSelect_Img = dynamic_cast
(PBtn_Role-> getChildByName ("Select_Img"); CC_BREAK_IF (pSelect_Img = NULL); if (pBtn_Role = pSender) {pSelect_Img-> setVisible (true ); m_nRoleID = I + 1; removeChildByTag (enTagRole); char cName [3] [256] = {"minren1", "xiaoyin", "NewProject"}; CCArmature * pRole = CCArmature:: create (cName [I]); CC_BREAK_IF (pRole = NULL); pRole-> setPosition (ccp (SCREEN_WIDTH/2,210 )); pRole-> setAnchorPoint (ccp (pRole-> getAnchorPoint (). x, 0); pRole-> getAnimation ()-> play ("hold",-1,-1,-1, 10000); addChild (pRole, enZOrderMid, enTagRole );} else {pSelect_Img-> setVisible (false) ;}return ;}while (false); CCLog ("CNFTeamCreateLayer: OnRoleSelect Error");} personal blog www.sundabke.com