cocos2dX UI控件之CCMenu

说道游戏, 有个我们不得不说的控件, 菜单控件(CCMenu), 平常我们与游戏很多的交互都来自于菜单, 菜单是游戏中不可或缺的一部分, 我们今天就来看看菜单控件, 

用法:

CCMenu::create( 菜单项1, 菜单项2..., NULL);

我们开始创建菜单( 旁白: 骗子, 根本就不行嘛)

别急, 我们先来看看参数, 哦  又是一个可变长参数, 结尾又要用NULL表示, 前面的参数是传进来的菜单项, 表示我们创建了哪些菜单

我们先来看看有哪些

CCMenuItemImage::create( "没有点击的时候显示的图片", "点击的时候显示的图片", 目标对象(一般写this), menu_selector(回调函数));//用来创建图片菜单

CCMenuItemFont::::create( "显示的文字",  目标对象(一般写this), menu_selector(回调函数));//用来创建文字菜单, 显示的文字也可以使用字体标签, 当然, 我们得换成CCMenuItemLabel

CCMenuItemToggle::createWithTarget( 目标对象(一般写this), menu_selector(回调函数), 可变长参数表(CCMenuItem对象), NULL);//用来创建开关菜单

我们找两张图片啊:


再写菜单之前, 我们创建两个函数( 旁白: 明明就是三个嘛), 用来点击菜单时候的调用这些函数:

在头文件里加上:

void menuCloseCallback(CCObject* pSender);
	void font( CCObject* pSender);
	void toggle( CCObject* pSender);

在cpp文件里实现:

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCLOG( "imageMenu");
}

void HelloWorld::font( CCObject* pSender)
{
	CCLOG( "<span style="font-family: Arial, Helvetica, sans-serif;">fontMenu</span><span style="font-family: Arial, Helvetica, sans-serif;">");</span>
}

void HelloWorld::toggle( CCObject* pSender)
{
	CCLOG( "<span style="font-family: Arial, Helvetica, sans-serif;">toggleMenu</span><span style="font-family: Arial, Helvetica, sans-serif;">");</span>
}


我在函数里就不实现过多的内容, 就在输出打印一下提示信息, 所以待会儿我们可以在输出里面看见我们打印的文字

CCMenuItemImage* imgMenu = CCMenuItemImage::create( "CloseNormal.png", 
		"CloseSelected.png", 
		this, 
		menu_selector(HelloWorld::menuCloseCallback));							//创建图片菜单
	CCMenuItemFont* fontMenu = CCMenuItemFont::create( "Play Game", 
		this, 
		menu_selector(HelloWorld::font));										//创建文字菜单
	CCMenuItemToggle* toggleMenu = CCMenuItemToggle::createWithTarget( this,	//创建开关菜单
		menu_selector(HelloWorld::toggle), 
		CCMenuItemFont::create( "on"), 
		CCMenuItemFont::create( "off"), 
		NULL);

	CCMenu* menu = CCMenu::create( imgMenu, fontMenu, toggleMenu, NULL);		//创建菜单, 将菜单项加载进来
	menu->setPosition( ccp( visibleSize.width / 2, visibleSize.height / 2));
	addChild( menu);


我们看看效果:


咦, 好像不对啊, 怎么说有的菜单都挤在一起了啊, 原来我们没有对每个菜单项设置位置, 我们可以对每个菜单项设置位置哦, 

其实, cocos2dX还为我们提供了其他解决办法, 例如纵向对其之类的,  看看这个

//	menu->alignItemsHorizontally();         // 垂直对齐
	menu->alignItemsVertically();           // 水平对象
//	menu->alignItemsInColumns(2, 1, NULL);  // 两行,两列,一列


看看, 这是个好东西吧, 自动就对齐了:


我们来点击每个菜单看看效果,


 点击图片菜单, 按下的时候两张图片切换了, 并且在输出打印了imgMenu, 

点击文字菜单, 文字放大, 并且输出打印了fontMenu

点击文字菜单, 文字改变, 并且输出打印了toggleMenu


嗯, 今天也差不多啦, CCMenu很实用简单的, 还是那句话, 希望大家多练习


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值