Quick-Cocos2d-x学习手册(二): 纯Lua实现CocoStudioGUI布局

本文介绍如何使用Lua创建CocoStudioGUI,并通过实例详细讲解容器控件UILayout的不同布局方式,包括绝对布局、水平布局、垂直布局及相对布局。

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

本文主要介绍如何用Lua创建CocoStudioGUI,并实现各种布局方式。


此文中,将用CocoStudio的UI编辑器制作、并能导入Cocos2d-x中使用的UI系统,称为CocoStudioGUI。以区别于Cocos2d-x中不完善的CC前缀UI系统。


就目前来说CocoStudioGUI当属所有Cocos UI系统中最佳的选择。

  • 作为Cocos2d-x内置的完整UI系统,它必然有着最大的用户群体,可以认为它经受住了严格的测试;

  • CocoStudioGUI可在编辑器中制作,这表示它的各属性已经在内部处理好依赖关系,任何的setter调用次序都不会有任何问题;

  • 使用CocoStudioGUI的工作流,能得到最高的开发效率。


考虑到Quick框架仍然处于不稳定开发状态,但其中的Cocos2d-x部分确是较为稳定的。因此个人认为使用CocoStudioGUI要优于quick自己的一套cc.ui.*包。


此文的内容介于基本控件和高级容器控件之间,通过例子,讲解容器控件UILayout的布局方式。


布局方式是制作前端界面的关键。浮动控件,变长控件列表以及UI屏幕适应都与此有关。理解不清楚CocoStudio的布局方式,后面的事情会很难做 ,甚至造成前期UI返工。因此专门测试其玩法,我也会比较放心。


基础容器UILayout

首先要建立一个容器控件,UILayout是最基本的容器类。在后面的例子中,它是场景居中的白色区域

1
2
3
4
5
6
7
8
local panel = Layout:create()
panel:setAnchorPoint(CCPoint(0, 0))
panel:setPosition(CCPoint(80, 80))
panel:setSize(CCSize(480-160, 320-160))
panel:setBackGroundColorType(LAYOUT_COLOR_SOLID)
panel:setBackGroundColor(ccc3(240, 240, 240))
panel:setLayoutType(LAYOUT_ABSOLUTE)
uiLayer:addChild(panel)


UILayout绝对布局

确保上述UILayout设置为水平模式

1
panel:setLayoutType(LAYOUT_LINEAR_HORIZONTAL)


添加三个子级图片

1
2
3
4
5
6
for  i=1,3  do
     local img = ImageView:create()
     img:loadTexture( "40.png" )
     panel:addChild(img)
     img:setPosition(ccp(40 * i, 40 * i))
end


效果如图:

icocos_02_absolute.png


UILayout水平布局

仅需修改panel:setLayoutType(…) 这行代码为

1
panel:setLayoutType(LAYOUT_LINEAR_HORIZONTAL)


大家会发现此时img:setPosition的调用变得不起作用了:

icocos_02_horizontal.png


UILayout从垂直布局

和上面一样,只需修改panel的布局模式

1
panel:setLayoutType(LAYOUT_LINEAR_VERTICAL)

icocos_02_vertical.png


UILayout相对布局

首先将panel改为相对布局模式

1
panel:setLayoutType(LAYOUT_LINEAR_VERTICAL)


但是,在设置字控件的布局参数时遇到了问题

1
2
3
4
5
6
local img = ImageView:create() 
img:loadTexture( "40.png"
local layoutParam = UIRelativeLayoutParameter:create() 
layoutParam:setAlign(RELATIVE_ALIGN_PARENT_TOP_RIGHT) 
img:setLayoutParameter(layoutParam)   
panel:addChild(img)

layoutParam:setAlign(…) 参数需要时一个UIRelativeAlign(C++中被typedef过的枚举),但是实际的枚举项RELATIVE-ALIGN-PARENT-TOP-RIGHT已被tolua转换为一个number。造成类型不匹配报错。


此问题有两种解法:

  1. 回避掉代码书写的方式,直接用CocoStudio的UI编辑器来设置好相对坐标

  2. 理解typedef枚举的转换方式。


推荐阅读:

Quick-Cocos2d-x 生存手册(一):游戏元素


来源网址:http://www.lolofinil.com/2014/07/09/icocos_02/#textlogo

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、付费专栏及课程。

余额充值