首页 > 代码库 > 分析Cocos2d-x横版ACT手游源代码 6、创建忍队界面

分析Cocos2d-x横版ACT手游源代码 6、创建忍队界面

.h头文件代码
#ifndef _NF_TEAM_CREATE_SCENE_HEAD_
#define _NF_TEAM_CREATE_SCENE_HEAD_

#include "../publicdef/PublicDef.h"

/************************************************************************/
/*创建忍队层															*/
/************************************************************************/
class CNFTeamCreateLayer : public CCLayer
{
protected:
	//标签
	enum 
	{
		enTagStudioMainUiLayer,
		enTagRole,
	};

	int				m_nRoleID;			//角色ID	1.鸣人	2.小樱	3.佐助

public:
	static CNFTeamCreateLayer * CreateLayer();
	static CCScene * scene();

	void OnGameStartPage(CCObject *pSender, TouchEventType type);				//创建人物

    void textFieldEvent(CCObject* pSender, TextFiledEventType type);

	void OnBackToLoginPage(CCObject *pSender, TouchEventType type);				//返回登录界面


	void OnRoleSelect(CCObject *pSender, TouchEventType type);					//角色选择界面

protected:
	virtual bool init();     
};

#endif
。cpp代码
#include "NFRoleSelectLayer.h"#include "NFTownScene.h"#include "NFLoginScene.h"#include "NFCreateTeamLayer.h"#include "datamanager/NFServerDataManager.h"/************************************************************************//*创建忍队层<span style="white-space:pre">															</span>*//************************************************************************/CCScene * CNFTeamCreateLayer::scene(){<span style="white-space:pre">	</span>do <span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>CCScene* pScene = CCScene::create();<span style="white-space:pre">		</span>CC_BREAK_IF(pScene==NULL);<span style="white-space:pre">		</span>CNFTeamCreateLayer* pLayer = CNFTeamCreateLayer::CreateLayer();<span style="white-space:pre">		</span>CC_BREAK_IF(pLayer==NULL);<span style="white-space:pre">		</span>pScene->addChild(pLayer);<span style="white-space:pre">		</span>return pScene;<span style="white-space:pre">	</span>} while (false);<span style="white-space:pre">	</span>CCLog("Fun CNFTeamCreateLayer::scene Error!");<span style="white-space:pre">	</span>return NULL;}CNFTeamCreateLayer * CNFTeamCreateLayer::CreateLayer(){<span style="white-space:pre">	</span>CNFTeamCreateLayer *pRet = new CNFTeamCreateLayer();<span style="white-space:pre">	</span>if (pRet && pRet->init())<span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>pRet->autorelease();<span style="white-space:pre">		</span>return pRet;<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>CCLog("Fun CNFTeamCreateLayer::CreateLayer Error!");<span style="white-space:pre">	</span>delete pRet; <span style="white-space:pre">	</span>pRet = NULL; <span style="white-space:pre">	</span>return NULL;}bool CNFTeamCreateLayer::init(){<span style="white-space:pre">	</span>do <span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>//初始化父类<span style="white-space:pre">		</span>CC_BREAK_IF(CCLayer::init()==false);<span style="white-space:pre">		</span>m_nRoleID = 1;<span style="white-space:pre">		</span>//创建主场景UI层<span style="white-space:pre">		</span>UILayer* pUiLayer = UILayer::create(); <span style="white-space:pre">		</span>CC_BREAK_IF(pUiLayer==NULL);<span style="white-space:pre">		</span>//-------------------加入主场景UI----------------------------<span style="white-space:pre">		</span>UILayout *pCreateNinja = dynamic_cast<UILayout*><span style="white-space:pre">	</span>(GUIReader::shareReader()->widgetFromJsonFile("CreateNinja.json"));<span style="white-space:pre">		</span>CC_BREAK_IF(pCreateNinja==NULL);<span style="white-space:pre">		</span>pUiLayer->addWidget(pCreateNinja);<span style="white-space:pre">		</span>pCreateNinja->setName("CreateNinjaLayer");<span style="white-space:pre">		</span>pCreateNinja->setSize(getContentSize());<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/xiaoyin.ExportJson");<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/NewProject.ExportJson");<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/minren1.ExportJson");<span style="white-space:pre">		</span>//获取返回按钮<span style="white-space:pre">		</span>UIButton *pBtn_Back=dynamic_cast<UIButton*>(pCreateNinja->getChildByName("MainUi")->getChildByName("Btn_Return"));<span style="white-space:pre">		</span>CC_BREAK_IF(pBtn_Back==NULL);<span style="white-space:pre">		</span>pBtn_Back->addTouchEventListener(this,toucheventselector(CNFTeamCreateLayer::OnBackToLoginPage));<span style="white-space:pre">		</span>//获取开始按钮<span style="white-space:pre">		</span>UIButton *pBtn_Create_Team=dynamic_cast<UIButton*>(pCreateNinja->getChildByName("MainUi")->getChildByName("Btn_Create_Team"));<span style="white-space:pre">		</span>CC_BREAK_IF(pBtn_Create_Team==NULL);<span style="white-space:pre">		</span>pBtn_Create_Team->addTouchEventListener(this,toucheventselector(CNFTeamCreateLayer::OnGameStartPage));<span style="white-space:pre">		</span>//获取输入框        UITextField* pTextField = dynamic_cast<UITextField*>(pCreateNinja->getChildByName("MainUi")->getChildByName("Team_Name_TextField"));<span style="white-space:pre">		</span>CC_BREAK_IF(pTextField==NULL);<span style="white-space:pre">		</span>pTextField->setTouchEnabled(true);        pTextField->addEventListenerTextField(this, textfieldeventselector(CNFTeamCreateLayer::textFieldEvent));<span style="white-space:pre">		</span>pTextField->setText("");<span style="white-space:pre">		</span>for (int i=0;i<3;i++)<span style="white-space:pre">		</span>{<span style="white-space:pre">			</span>//获取人物按钮<span style="white-space:pre">			</span>UIButton *pBtn_Role=dynamic_cast<UIButton*>(pCreateNinja->getChildByName("MainUi")->getChildByName(CCString::createWithFormat("Btn_Char_%d",i+1)->getCString()));<span style="white-space:pre">			</span>CC_BREAK_IF(pBtn_Role==NULL);<span style="white-space:pre">			</span>pBtn_Role->addTouchEventListener(this,toucheventselector(CNFTeamCreateLayer::OnRoleSelect));<span style="white-space:pre">			</span>if (i==0)<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>//获取按下图片<span style="white-space:pre">				</span>UIImageView *pSelect_Img=dynamic_cast<UIImageView*>(pBtn_Role->getChildByName("Select_Img"));<span style="white-space:pre">				</span>CC_BREAK_IF(pSelect_Img==NULL);<span style="white-space:pre">				</span>pSelect_Img->setVisible(true);<span style="white-space:pre">			</span>}<span style="white-space:pre">		</span>}<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/xiaoyin.ExportJson");<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/NewProject.ExportJson");<span style="white-space:pre">		</span>CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/minren1.ExportJson");<span style="white-space:pre">		</span>char cName[3][256]={"minren1","xiaoyin","NewProject"};<span style="white-space:pre">		</span>CCArmature *pRole = CCArmature::create(cName[0]);<span style="white-space:pre">		</span>CC_BREAK_IF(pRole==NULL);<span style="white-space:pre">		</span>pRole->setPosition(ccp(SCREEN_WIDTH/2,210));<span style="white-space:pre">		</span>pRole->setAnchorPoint(ccp(pRole->getAnchorPoint().x,0));<span style="white-space:pre">		</span>pRole->getAnimation()->play("hold",-1,-1,-1,10000);<span style="white-space:pre">		</span>addChild(pRole,enZOrderMid,enTagRole);<span style="white-space:pre">		</span><span style="white-space:pre">		</span>addChild(pUiLayer,enZOrderBack,enTagStudioMainUiLayer);<span style="white-space:pre">		</span>return true;<span style="white-space:pre">	</span>} while (false);<span style="white-space:pre">	</span>CCLog("Fun CNFTeamCreateLayer::init Error!");<span style="white-space:pre">	</span>return false;}void CNFTeamCreateLayer::OnGameStartPage( CCObject *pSender, TouchEventType type ){<span style="white-space:pre">	</span>do <span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>switch (type)<span style="white-space:pre">		</span>{<span style="white-space:pre">		</span>case TOUCH_EVENT_ENDED:<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>if(m_nRoleID==0)return;<span style="white-space:pre">				</span>UILayer* pMainUiLayer = dynamic_cast<UILayer*>(getChildByTag(enTagStudioMainUiLayer));<span style="white-space:pre">				</span>CC_BREAK_IF(pMainUiLayer==NULL);<span style="white-space:pre">				</span>UILayout *pCreateNinja = dynamic_cast<UILayout*>(pMainUiLayer->getWidgetByName("CreateNinjaLayer"));<span style="white-space:pre">				</span>CC_BREAK_IF(pCreateNinja==NULL);<span style="white-space:pre">				</span>//获取输入框<span style="white-space:pre">				</span>UITextField* pTextField = dynamic_cast<UITextField*>(pCreateNinja->getChildByName("MainUi")->getChildByName("Team_Name_TextField"));<span style="white-space:pre">				</span>CC_BREAK_IF(pTextField==NULL);<span style="white-space:pre">				</span>if(strcmp(pTextField->getStringValue(),"")==0)return;<span style="white-space:pre">	</span><span style="white-space:pre">				</span>//读取角色信息<span style="white-space:pre">				</span>CNFServerRoleSelectInfoVec RoleSelectInfoVec;<span style="white-space:pre">				</span>CNFServerDataManager::SharedData()->GetRoleSelectInfo(RoleSelectInfoVec);<span style="white-space:pre">				</span>CNFServerRoleSelectInfoVec::iterator itRole=RoleSelectInfoVec.begin();<span style="white-space:pre">				</span>for (;itRole!=RoleSelectInfoVec.end();itRole++)<span style="white-space:pre">				</span>{<span style="white-space:pre">					</span>if (itRole->nRoleID==m_nRoleID)<span style="white-space:pre">					</span>{<span style="white-space:pre">						</span>itRole->nState=0;<span style="white-space:pre">					</span>}<span style="white-space:pre">				</span>}<span style="white-space:pre">				</span>CNFServerDataManager::SharedData()->SetRoleSelectInfo(RoleSelectInfoVec);<span style="white-space:pre">				</span>//人物选择场景<span style="white-space:pre">				</span>CCScene * pScene = CNFRoleSelectLayer::scene();<span style="white-space:pre">				</span>CC_BREAK_IF(pScene==NULL);<span style="white-space:pre">				</span>CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.f,pScene));<span style="white-space:pre">			</span>}break;<span style="white-space:pre">		</span>default:<span style="white-space:pre">			</span>break;<span style="white-space:pre">		</span>}<span style="white-space:pre">		</span>return;<span style="white-space:pre">	</span>} while (false);<span style="white-space:pre">	</span>CCLog("CNFTeamCreateLayer::OnGameStartPage Error");}void CNFTeamCreateLayer::textFieldEvent( CCObject* pSender, TextFiledEventType type ){}void CNFTeamCreateLayer::OnBackToLoginPage( CCObject *pSender, TouchEventType type ){<span style="white-space:pre">	</span>do <span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>//跳转到登陆界面<span style="white-space:pre">		</span>CCScene * pScene = CNFLoginScene::scene();<span style="white-space:pre">		</span>CC_BREAK_IF(pScene==NULL);<span style="white-space:pre">		</span>CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.f,pScene));<span style="white-space:pre">		</span>return;<span style="white-space:pre">	</span>} while (false);<span style="white-space:pre">	</span>CCLog("CNFTeamCreateLayer::OnBackToLoginPage Error");}void CNFTeamCreateLayer::OnRoleSelect( CCObject *pSender, TouchEventType type ){<span style="white-space:pre">	</span>do <span style="white-space:pre">	</span>{<span style="white-space:pre">		</span>UILayer* pMainUiLayer = dynamic_cast<UILayer*>(getChildByTag(enTagStudioMainUiLayer));<span style="white-space:pre">		</span>CC_BREAK_IF(pMainUiLayer==NULL);<span style="white-space:pre">		</span>UILayout *pCreateNinja = dynamic_cast<UILayout*><span style="white-space:pre">	</span>(pMainUiLayer->getWidgetByName("CreateNinjaLayer"));<span style="white-space:pre">		</span>CC_BREAK_IF(pCreateNinja==NULL);<span style="white-space:pre">		</span>for (int i=0;i<3;i++)<span style="white-space:pre">		</span>{<span style="white-space:pre">			</span>//获取人物按钮<span style="white-space:pre">			</span>UIButton *pBtn_Role=dynamic_cast<UIButton*>(pCreateNinja->getChildByName("MainUi")->getChildByName(CCString::createWithFormat("Btn_Char_%d",i+1)->getCString()));<span style="white-space:pre">			</span>CC_BREAK_IF(pBtn_Role==NULL);<span style="white-space:pre">			</span>//获取按下图片<span style="white-space:pre">			</span>UIImageView *pSelect_Img=dynamic_cast<UIImageView*>(pBtn_Role->getChildByName("Select_Img"));<span style="white-space:pre">			</span>CC_BREAK_IF(pSelect_Img==NULL);<span style="white-space:pre">			</span>if (pBtn_Role==pSender)<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>pSelect_Img->setVisible(true);<span style="white-space:pre">				</span>m_nRoleID=i+1;<span style="white-space:pre">				</span>removeChildByTag(enTagRole);<span style="white-space:pre">				</span>char cName[3][256]={"minren1","xiaoyin","NewProject"};<span style="white-space:pre">				</span>CCArmature *pRole = CCArmature::create(cName[i]);<span style="white-space:pre">				</span>CC_BREAK_IF(pRole==NULL);<span style="white-space:pre">				</span>pRole->setPosition(ccp(SCREEN_WIDTH/2,210));<span style="white-space:pre">				</span>pRole->setAnchorPoint(ccp(pRole->getAnchorPoint().x,0));<span style="white-space:pre">				</span>pRole->getAnimation()->play("hold",-1,-1,-1,10000);<span style="white-space:pre">				</span>addChild(pRole,enZOrderMid,enTagRole);<span style="white-space:pre">			</span>}<span style="white-space:pre">			</span>else<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>pSelect_Img->setVisible(false);<span style="white-space:pre">			</span>}<span style="white-space:pre">		</span>}<span style="white-space:pre">		</span>return;<span style="white-space:pre">	</span>} while (false);<span style="white-space:pre">	</span>CCLog("CNFTeamCreateLayer::OnRoleSelect Error");}个人博客 www.sundaboke.com

分析Cocos2d-x横版ACT手游源代码 6、创建忍队界面