千家信息网

cocos2dx3.2如何实现格斗人物选择界面

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,本篇文章给大家分享的是有关cocos2dx3.2如何实现格斗人物选择界面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。先看最终实现的效果
千家信息网最后更新 2025年01月23日cocos2dx3.2如何实现格斗人物选择界面

本篇文章给大家分享的是有关cocos2dx3.2如何实现格斗人物选择界面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

先看最终实现的效果图:

创建以人物选择场景 ChooseHero.h

#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);//创建人物void textFieldEvent(Object* pSender, Event type);//void OnBackToLoginPage(Object *pSender, Event type);//返回登录界面void OnRoleSelect(CCObject *pSender, Widget::TouchEventType type);//角色选择界面void OnRoleSelect2(CCObject *pSender, Widget::TouchEventType type);//角色选择界面void OnRoleSelect3(CCObject *pSender, Widget::TouchEventType type);//角色选择界面bool OnRoleSelectMy(Touch *touch, Event *event);CREATE_FUNC(ChooseHero);protected://标签enum{enTagStudioMainUiLayer,enTagRole,};intm_nRoleID;//角色ID 1.鸣人2.小樱3.佐助int btnIndex2;Button* pBtn_Role ;Button* pBtn_Role2;Button* pBtn_Role3;ImageView * pSelect_Img ;ImageView * pSelect_Img2;ImageView * pSelect_Img3;};#endif

init实现如下:

bool ChooseHero::init(){if (!Layer::init()){return false;}m_nRoleID = 1;//创建主场景UI层auto pCreateNinja = GUIReader::getInstance()->widgetFromJsonFile("CreateNinja.json");addChild(pCreateNinja, 100, 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");////获取开始按钮Button *pBtn_Create_Team = dynamic_cast(pCreateNinja->getChildByName("MainUi")->getChildByName("Btn_Create_Team"));pBtn_Create_Team->addTouchEventListener(this, toucheventselector(ChooseHero::OnGameStartPage));////获取输入框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("");//获取人物按钮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/ 2, 210));pRole->setAnchorPoint(ccp(pRole->getAnchorPoint().x, 0));pRole->getAnimation()->play("hold");addChild(pRole, 100000, enTagRole);return true;}

回调方法实现如下:

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;removeChildByTag(enTagRole);char cName[3][256] = { "minren1", "xiaoyin", "NewProject" };Armature *pRole = Armature::create(cName[0]);pRole->setPosition(ccp(640 / 2, 210));pRole->setAnchorPoint(ccp(pRole->getAnchorPoint().x, 0));pRole->getAnimation()->play("hold");addChild(pRole, 100000, enTagRole);}else{pSelect_Img->setVisible(false);}}

以上就是cocos2dx3.2如何实现格斗人物选择界面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

0