cocos2dx3.x《格斗》类游戏实现人物选择界面(二)
来源:互联网 发布:mac ladybug试色 编辑:程序博客网 时间:2024/05/16 15:38
先看最终实现的效果图:
由于原本2.x版本的监听事件移植到3.x上面,或许是API改了什么也不知道啊。
创建一个人物选择场景。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); //角色选择界面 CREATE_FUNC(ChooseHero);protected: //标签 enum { enTagStudioMainUiLayer, enTagRole, }; int m_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<Button*>(pCreateNinja->getChildByName("MainUi")->getChildByName("Btn_Create_Team")); pBtn_Create_Team->addTouchEventListener(this, toucheventselector(ChooseHero::OnGameStartPage)); ////获取输入框 TextField* pTextField = dynamic_cast<TextField*>(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<Button*>(pCreateNinja->getChildByName("MainUi")->getChildByName(String::createWithFormat("Btn_Char_%d", 0 + 1)->getCString())); pBtn_Role2 = static_cast<Button*>(pCreateNinja->getChildByName("MainUi")->getChildByName(String::createWithFormat("Btn_Char_%d", 1 + 1)->getCString())); pBtn_Role3 = static_cast<Button*>(pCreateNinja->getChildByName("MainUi")->getChildByName(String::createWithFormat("Btn_Char_%d", 2 + 1)->getCString())); pSelect_Img = static_cast<ImageView*>(pBtn_Role->getChildByName("Select_Img")); pSelect_Img2 = static_cast<ImageView*>(pBtn_Role2->getChildByName("Select_Img")); pSelect_Img3 = static_cast<ImageView*>(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); }}
可参考源码:https://github.com/zhenqicai/GeDou 或者http://git.oschina.net/zhenqi/Gedou
阅读全文
0 0
- cocos2dx3.x《格斗》类游戏实现人物选择界面(二)
- cocos2dx3.x《格斗》类游戏加载界面(一)
- Cocos2d-x游戏开发《赵云要格斗》 (二) cocos2dx3.3 - _ - ||貌似偷懒了
- Cocos2d-x游戏开发《赵云要格斗》 (一) cocos2dx3.3
- Cocos2d-x游戏暂停、继续游戏、重新开始界面的实现---之游戏开发《赵云要格斗》(10)
- Cocos2d-X 3.4版-游戏继续,游戏重新开始,回到主界面的实现《赵云要格斗》
- Cocos2d-x使用Cocos Studio制作界面并应用---之游戏开发《赵云要格斗》(11)
- 如何制作一个横版格斗过关游戏(二) Cocos2d-x 2.0.4
- Cocos2d-x自定义按钮类控制精灵攻击----之游戏开发《赵云要格斗》(2)
- Cocos2d-x格斗小游戏(二) 晃动的玩家精灵
- Cocos2d-x自定义按钮类控制精灵攻击----之游戏开发《赵云要格斗》(2) cocos2dx 3.3移植版
- cocos2dx3.X项目重写(二)新的物理引擎
- cocos2dx3.X项目重写(二)新的物理引擎
- 分析Cocos2d-x横版ACT手游源代码 3、人物选择界面
- 如何制作一个横版格斗过关游戏(一) Cocos2d-x 2.0.4
- Cocos2d-x虚拟摇杆控制精灵上下左右运动----之游戏开发《赵云要格斗》(1)
- Cocos2d-x 自定义血条及其美化----之游戏开发《赵云要格斗》(4)
- Cocos2d-x血条跟随怪物运动----之游戏开发《赵云要格斗》(5)
- 位域与结构体
- XLDs-2
- URAL1325-Dirt
- SBT
- 用canvas的toDataURL()将图片转为dataURL(base64)
- cocos2dx3.x《格斗》类游戏实现人物选择界面(二)
- String对象方法
- HangFire任务调度实例(Console和MVC中)及Log4Net日志配置
- 浅谈CTF中命令执行与绕过的小技巧
- 边数限制最短路
- JSP内置对象
- ERROR: Can't get master address from ZooKeeper; znode data == null
- Android ViewPager+RecyclerView+StaggerLayoutManager瀑布流图片单选实现
- 利用域名在前端取图片