(转)如何使用 CocoStudio UI 编辑器实现设置界面并且包括代码

本文详细介绍了如何使用CocoStudioUI编辑器设计游戏配置界面,并通过编写代码实现场景逻辑。从素材准备、界面布局、交互实现到功能控制,全方位指导开发者构建高效、美观的UI系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 游戏中必不可少的 UI 元素

./cocostudioUI/image04.png

一个成功的游戏离不开友好的用户体验,而用户体验则取决于功能是否合理,界面是否美观等因素,除了游戏的核心玩法之外,游戏中的各种“配置功能”也是必不可缺的,更准确的说,游戏中存在那么些必不可少的 UI 元素。

上图所示是近期非常火爆的游戏《乱斗堂》的截图,内容是其“配置界面”,如果要实现这样一个“配置界面”,如果是你,你该从何下手!首先要有素材(当然请美工制作了),然后我们编写代码,这样一个界面我们可以用一个层来实现,加载各种图片素材,逐一添加至层。其实,这都没什么,只是你需要手动设置坐标,不断的运行调试,才能达到最终想要的效果而已。效果出来了,实现具体的功能,点击操作该如何实现,如果所有显示的图片都是精灵,那么你需要做触摸处理,判断点击有效否,然后可能需要修改精灵图片(不同的点击效果),聪明一点的做法是实用 CCMenu 来实现点击功能,但如此可能会引入其它问题,CCMenu 的触摸优先级别很高,以至于多层 UI 的情况,处理起来变得繁杂。而更聪明一点的做法,就是设计一套 UI 系统,来满足各种需求!

我们在编写游戏之前还需要实现一套自己的 UI 系统?当然不是,即便是 Cocos2d 的第一个 python 版本,也不是一夕而就的,而是开发多个游戏之后总结、规范、封装重用之后的框架,而 UI 系统也符合这么个客观规律。都是为了解决实际开发过程中遇到的问题,重用相同的功能。简化我们的开发。

下面介绍如何使用 CocoStudio 的 UI 编辑器来帮助我们实现这样一个“配置界面”,并且实现其配置功能。

2 使用 CocoStudio UI 编辑器设计配置界面

2.1 首先建立主配置界面:

  1. 安装好 CocoStudio 程序,并准备好所需要的素材
    QQ截图20130724172821
  2. 建立新的项目,命名“ChaosFight”,设置分辨率(根据实际需要),这里手动填写分辨率。
  3. 导入游戏的素材到项目,在界面添加图片框控件,显示背景./cocostudioUI/image06.png
  4. 根据需要添加控件,在这个主界面上我们添加了,一个图片框,下面四个文本按钮,再下面一排四个图片按钮,最下面是两个文本按钮和一个文本框(文本域)。
    ./cocostudioUI/image07.png
  5. 在编辑时,我们需要注意以下几点:
    • 设置图片按钮之时,可以设置禁用时显示的图片。所有的可点击操作的控件,需要启用“交互”属性。
    • 设置按钮属性 默认图片 与 点击效果图 的图片相同(或者不同,按下效果图如果不设置,实际操作按下也不显示,空白)

    QQ截图20130724172132

  6. 导出各部分资源文件

2.2 其次我们需要一个 “修改密码” 的二级 UI 界面:

  1. 新建立项目,并导入相关资源。
  2. 设计界面,如下图所示:
    ./cocostudioUI/image02.png
    这里我们添加了三组密码框。而在设计这样三个类似控件集的时候,有个技巧,我们首先局部好一个个控件区域,如上“旧密码”区域,然后我们将相关的控件树结构,统一在一起,如图:
    ./cocostudioUI/image01.png
    QQ截图20130724171717

    点击右侧对象结构,我们可以复制整个树枝“节点”,然后粘贴到树中,如上图,“新密码”区域,我们将相关的控件集合在“新密码”节点,然后拖动此节点,可以很好的完成内部元素的相对位置。
  3. 修改相关属性,命名规范并导出资源

以上时设计界面的简单步骤,所有的都是可视化操作,所见即所得,已经提供了常用的控件,并且还在不断添加完善。

3 编写代码控制页面逻辑

  1. 建立新的工程,引入 CocoGUILIB 扩展库,和 UI 编辑器导出的资源文件(工程建立步骤请实时关注官方说明,不同版本操作步骤不同,这里使用的时 2.1.4e 版本,请下载最新的版本库,以使用最简单的方法配置环境等。)
  2. 创建一个新的场景类,用于加载我们的 UI 资源,并编写相关逻辑,其关键代码如下(实现加载,跳转逻辑控制功能) 所有代码即工程资源:
  3.  工程代码下载 地址:点击下载整个工程

 

#ifndef TestCpp_ChaosFight_h
#define TestCpp_ChaosFight_h

#include "cocos2d.h"
#include "CocosGUI.h"

USING_NS_CC;
USING_NS_CC_EXT;

class ChaosFightUI: public CCLayer{
public:
    static CCScene* scene();
    virtual bool init();
    CREATE_FUNC(ChaosFightUI);
    
    void tbChangePwdCallback(CCObject* pSender);
    void tbBindingEmailCallback(CCObject* pSender);
    void tbChangeRoleCallback(CCObject* pSender);
    void tbLogoutCallback(CCObject* pSender);
    
    void btnSoundEffectCallback(CCObject* pSender);
    void btnMusicEffectCallback(CCObject* pSender);
    void btnSavingElectricityCallback(CCObject* pSender);
    void btnVideoCallback(CCObject* pSender);
    
    void tbAboutCallback(CCObject* pSender);
    void tbClearCacheCallback(CCObject* pSender);
    
    
    void btnXCallback(CCObject* pSender);
    void tbOkCallback(CCObject* pSender);

private:
    UILayer* ul;
    
    
    UILayer* ulPwd;
    
    UIButton* btnX;
    UITextButton* tbOk;
    
    UITextField* tfOldPwd;
    UITextField* tfNewPwd;
    UITextField* tfNewPwdConfirm;
    
};


#endif

 

    实现代码

 

#include "ChaosFight.h"

CCScene* ChaosFightUI::scene(){
    CCScene* scene = CCScene::create();
    CCLayer* layer = ChaosFightUI::create();
    scene->addChild(layer);
    return scene;
}

bool ChaosFightUI::init(){
    bool bRef = false;
    
    do{
        CC_BREAK_IF(! CCLayer::init());
        
        ul = UILayer::create();
        // 设置 UI 层的tag
        this->addChild(ul, 0, 100);
        ul->addWidget(CCUIHELPER->createWidgetFromJsonFile("ChaosFight_1/ChaosFight_1.json"));
        
        // 获得各个控件,并添加点击事件
        UITextButton* tbChangePwd = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbChangePwd"));
        UITextButton* tbBindingEmail = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbBindingEmail"));
        UITextButton* tbChangeRole = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbChangeRole"));
        UITextButton* tbLogout = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbLogout"));
        
        
        UIButton* btnSoundEffect = dynamic_cast<UIButton*>(ul->getWidgetByName("btnSoundEffect"));
        UIButton* btnMusic = dynamic_cast<UIButton*>(ul->getWidgetByName("btnMusic"));
        UIButton* btnSavingElectricity = dynamic_cast<UIButton*>(ul->getWidgetByName("btnSavingElectricity"));
        UIButton* btnVideo = dynamic_cast<UIButton*>(ul->getWidgetByName("btnVideo"));
        
        UITextButton* tbAbout = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbAbout"));
        UITextButton* tbClearCache = dynamic_cast<UITextButton*>(ul->getWidgetByName("tbClearCache"));
        
        // 设置字体颜色
        tbChangePwd->setTextColor(0, 0, 0);
        tbBindingEmail->setTextColor(0, 0, 0);
        tbChangeRole->setTextColor(0, 0, 0);
        tbLogout->setTextColor(0, 0, 0);
        
        tbAbout->setTextColor(0, 0, 0);
        tbClearCache->setTextColor(0, 0, 0);
        
        // 为控件添加处理事件
        tbChangePwd->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbChangePwdCallback));
        tbBindingEmail->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbBindingEmailCallback));
        tbChangeRole->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbChangeRoleCallback));
        tbLogout->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbLogoutCallback));

        btnSoundEffect->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::btnSoundEffectCallback));
        btnMusic->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::btnMusicEffectCallback));
        btnSavingElectricity->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::btnSavingElectricityCallback));
        btnVideo->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::btnVideoCallback));

        tbAbout->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbAboutCallback));
        tbClearCache->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbClearCacheCallback));
        
        
        bRef = true;
    }while(0);
    return bRef;
}

void ChaosFightUI::tbChangePwdCallback(cocos2d::CCObject *pSender){
    // 创建加载修改密码界面 ulPwd 作为类成员属性,以便重用  
    ulPwd = UILayer::create();
    ulPwd->addWidget(CCUIHELPER->createWidgetFromJsonFile("ChaosFightPassword_1/ChaosFightPassword_1.json"));
    this->addChild(ulPwd);
    ulPwd->setAnchorPoint(CCPoint(0.5,0.5));
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    ulPwd->setPosition(CCPoint(winSize.width / 2 - 250, winSize.height / 2 - 180));
    
    // 获取点击确定按钮
    tbOk = dynamic_cast<UITextButton*>(ulPwd->getWidgetByName("tbOk"));
    tbOk->addReleaseEvent(this, coco_releaseselector(ChaosFightUI::tbOkCallback));
    tbOk->setTextColor(0, 0, 0);
    
    tfOldPwd = dynamic_cast<UITextField*>(ulPwd->getWidgetByName("tfOldPwd"));
    tfNewPwd = dynamic_cast<UITextField*>(ulPwd->getWidgetByName("tfNewPwd"));
    tfNewPwdConfirm = dynamic_cast<UITextField*>(ulPwd->getWidgetByName("tfNewPwdConfirm"));
    
    tfOldPwd->setColor(ccc3(0, 0, 0));
    tfNewPwd->setColor(ccc3(0, 0, 0));
    tfNewPwdConfirm->setColor(ccc3(0, 0, 0));
    

    ul->setTouchEnabled(false);

}

void ChaosFightUI::tbBindingEmailCallback(cocos2d::CCObject *pSender){
    CCMessageBox("绑定邮箱", "title");
}

void ChaosFightUI::tbChangeRoleCallback(cocos2d::CCObject* pSender){
    CCMessageBox("切换角色", "title");
}

void ChaosFightUI::tbLogoutCallback(cocos2d::CCObject *pSender){
    CCMessageBox("注销", "title");
}

void ChaosFightUI::btnSoundEffectCallback(cocos2d::CCObject *pSender){
    CCMessageBox("音效", "title");
}

void ChaosFightUI::btnMusicEffectCallback(cocos2d::CCObject *pSender){
    CCMessageBox("音乐", "title");
}

void ChaosFightUI::btnSavingElectricityCallback(cocos2d::CCObject *pSender){
    CCMessageBox("省电", "title");
}

void ChaosFightUI::btnVideoCallback(cocos2d::CCObject *pSender){
    CCMessageBox("片头", "title");
}

void ChaosFightUI::tbAboutCallback(cocos2d::CCObject *pSender){
    CCMessageBox("关于", "title");
}

void ChaosFightUI::tbClearCacheCallback(cocos2d::CCObject *pSender){
    CCMessageBox("清楚缓存", "title");
}

void ChaosFightUI::btnXCallback(cocos2d::CCObject *pSender){
    CCMessageBox("btnX", "title");
}

void ChaosFightUI::tbOkCallback(cocos2d::CCObject *pSender){
    // 获取文本框值,并且打印
    const char* value = tfOldPwd->getStringValue();
    CCLog(value);
    ul->setTouchEnabled(true);
    this->removeChild(ulPwd);
}

 

 

 

  1. 最后运行效果如下:./cocostudioUI/image03.png二级 UI 界面:./cocostudioUI/image00.png文本输入框控件:./cocostudioUI/image05.png

 

About CocoStudio is a game development tool kit based on Cocos2d-x. It breaks down tasks in game development into different roles, it includes: UI editor for UI graphic artists, Animation editor for graphic artists, Number cruncher for game data designers, Scene editor for game designers CocoStudio forms a complete game development solution. The UI editor The UI was designed to serve its only purpose: create UI for games. Its simple and intuitive interface allows graphic artists to focus on their expertise, without worrying about other aspects such as programming. Currently the UI editor has 12 different UI elements ready to be used in games, new UI elements will be added with each and every release of CocoStudio, Other key features that the UI editor supports are: Texture packaging - Automatically packs individual texture files into a single large sprite, which further saves memory and improves game performance. Multi-resolution adaption - Automatically adapts to multiple resolution sizes with relative UI positioning. Templating - Reuse the same UI layout across different games, swap out texture resources to give it a new look. The Animation editor The Animation editor was designed to feel like Adobe Flash, which makes graphic artists feel right at home. The Animation editor brings skeletal animation to Cocos2d-x. What advantage does skeletal animation holds against the traditional frame animation? Lower memory consumption - An animation with the traditional frame based solution could use dozens of individual textures, but with skeletal animation, only 1 set of body parts is required to make infinite number of different animations. Smaller file size - due to less number of assets. Animation blending - you can combine animations together to easily make new animation, for example, you could blend attacking animation with walk animation to create "attacking while walking animation". Animation reuse - you can share skeletal animations with another character with the same skeleton setup. Smooth interpolation - traditional frame based animation is very choppy especially in slow motion. Skeletal animation interpolates between 2 sets of key frames, so animation is always played at the same frame rate as the game. However Skeletal animation cannot replace the traditional frame based animation, for example, it cannot make isometric character, it cannot make explosion, that is why we did not forget frame based animation, we even made it better and simpler. You only have to drag and drop frame sequences to the work space, and the animation editor will automatically creates the frame animation for you. Other highlight of Animation editor includes: WYSIWYG collision box editing - editing collision box in wysiwyg way has never being easier and accurate. Reference point - enables characters to wield swords, mount horses, and attaching other objects easily. Texture packing - Automatically packs individual texture files into a single large sprite, which further saves memory and improves game performance. The Data Cruncher The data Cruncher imports excel tables and converts the data into a format readable by cocos2d-x, which can also be used as a component for the Scene editor. The Scene editor The scene editor pieces all the assets made by the UI editor, Animation editor, and the Data Cruncher into a game scene, it can then simulate the game inside the editor. The scene editor also supports many assets made from third party editors such as particle designer, tiled etc. The scene editor relies on the CocosStudio Framework. CocoStudio Framework CocoStudio Framework is an open source higher level framework on top of Cocos2d-x, it employes entity and component system, it is used to read the data saved from the scene editor. The Scene editor saves data in a MVC like fashion, it includes all entities and events used in the current scene, and exports to corresponding code template for the programmers. A programmer can then write the code in Javascript to bring the game alive. CocoStudio的安装 1.CocoStudio的运行平台是Windows操作系统,推荐使用Windows7操作系统。 2.安装CocoStudio之前,确保电脑中安装了.Net 4.0 Framework 3.安装目录尽量不要在C盘的Program Files文件夹下,可能会导致启动器无法启动编辑器。当然,通过“以管理员身份运行”的方式也可以打开软件 4.在Xp和Windows8的操作系统下,可能会出现的闪屏或无法运行的问题。这个问题会尽快修复
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值